﻿using System;
using System.ComponentModel;
using System.Deployment.Application;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Windows.Forms;
using AGaugeApp;
using UsbLibrary;
using WeatherAnywhere;
using WeatherAnywhere.Wmr200;
using DataReceivedEventArgs = UsbLibrary.DataReceivedEventArgs;
using DataReceivedEventHandler = UsbLibrary.DataReceivedEventHandler;

namespace WMR_Data_Logger
{
    public class Wmr200DataLogger : Form
    {
        private readonly string ProdVersion = "";
        private readonly bool autoStartWD;
        private readonly byte[] rcv_data = new byte[0x100];
        private readonly byte[] wmr_data = new byte[0x100];
        private int WMR_timeout = 30;
        private ToolStripMenuItem aboutToolStripMenuItem;
        private bool autoStart;
        private decimal avgTime = 0.75M;
        private Button btnClear;
        private Button btnConnect;
        private Button btn_copy;
        private Button btn_copy_all;
        private Button btn_erase;
        private Button btn_reset;
        private Button btn_save;
        private Button btn_send;
        private Button btn_sendD0;
        private Button btn_sendDA;
        private Button btn_sendDF;
        private Button btn_start;
        private Button btn_stop;
        private Button btn_wdexport;
        private CheckBox cb_ackLIVE;
        private CheckBox cb_heartbeat;
        private CheckBox cb_scroll;
        private CheckBox cb_showUSB;
        private CheckBox cb_showWMR;
        private IContainer components;
        private ToolStripMenuItem configToolStripMenuItem;
        private string data_Dir = "";
        private decimal datalogger_interval = 1M;
        private string dd;
        private DataGridView dg_history;
        private SaveFileDialog dlg_save;
        private ErrorProvider errorProvider1;
        private TimeSpan estTime = new TimeSpan(0L);
        private ToolStripMenuItem fileToolStripMenuItem;
        private AGauge gauge_rpm;
        private GroupBox gb_counts;
        private GroupBox gb_dataLogger;
        private GroupBox gb_manual;
        private GroupBox gb_usb_data;
        protected GroupBox gb_wx_data;
        private bool gotTimeFix;
        private int heartbeatCount;
        private int heartbeat_interval = 0x1d;
        private string hh;
        private decimal histTimeFix;
        private DateTime hist_from_date;
        private int iBaro;
        private int iHist;
        private int iJunk;
        private int iLive;
        private int iRain;
        private int iStat;
        private int iTemp;
        private int iTotal;
        private int iUVI;
        private int iWind;
        private bool inHistory = true;
        private bool kickstartOnTimeout;
        private Label lb_baro;
        private Label lb_baro_h;
        private Label lb_batt;
        private Label lb_batt_h;
        private Label lb_corrupt;
        private Label lb_cur_date;
        private Label lb_cur_h;
        private Label lb_cur_time;
        private Label lb_done;
        private Label lb_end_date;
        private Label lb_end_h;
        private Label lb_end_time;
        private Label lb_heartbeatCount;
        private Label lb_hist;
        private Label lb_hist_h;
        private Label lb_junk;
        private ListBox lb_message;
        private Label lb_missing;
        private Label lb_missing_h;
        private Label lb_percent;
        private Label lb_product;
        private Label lb_rain;
        private Label lb_rain_h;
        private Label lb_rec_done;
        private Label lb_rec_rpm;
        private Label lb_rec_time;
        private Label lb_records_h;
        private Label lb_seconds_h;
        private Label lb_start_date;
        private Label lb_start_h;
        private Label lb_start_time;
        private Label lb_temp;
        private Label lb_temp_h;
        private Label lb_timeLeft;
        private Label lb_total;
        private Label lb_total_h;
        private Label lb_uvi;
        private Label lb_uvi_h;
        private Label lb_vendor;
        private Label lb_wind;
        private Label lb_wind_h;
        private DateTime logTime;
        private MenuStrip menuStrip1;
        private string mm;
        private string mn;
        private DateTime nowTime;
        private ProgressBar pb_done;
        private PictureBox pictureBox1;
        private DataGridViewTextBoxColumn pkt_baro;
        private DataGridViewTextBoxColumn pkt_day;
        private DataGridViewTextBoxColumn pkt_dewp;
        private DataGridViewTextBoxColumn pkt_hindex;
        private DataGridViewTextBoxColumn pkt_hour;
        private DataGridViewTextBoxColumn pkt_hum;
        private DataGridViewTextBoxColumn pkt_min;
        private DataGridViewTextBoxColumn pkt_mth;
        private DataGridViewTextBoxColumn pkt_rainDay;
        private DataGridViewTextBoxColumn pkt_rainMin;
        private DataGridViewTextBoxColumn pkt_rainMth;
        private DataGridViewTextBoxColumn pkt_rainYear;
        private DataGridViewTextBoxColumn pkt_temp;
        private string pkt_time = "";
        private DataGridViewTextBoxColumn pkt_windDir;
        private DataGridViewTextBoxColumn pkt_windGst;
        private DataGridViewTextBoxColumn pkt_windSpd;
        private DataGridViewTextBoxColumn pkt_year;
        private ToolStripMenuItem quitToolStripMenuItem;
        private RadioButton rb_DL1;
        private RadioButton rb_DL10;
        private RadioButton rb_DL2;
        private RadioButton rb_DL5;
        private RadioButton rb_ackDA;
        private RadioButton rb_ackNONE;
        private RichTextBox re_box;
        private string rec_type = "Data: ";
        private decimal remainingSecs;
        private decimal rpm;
        private bool showDataGrid = true;
        private bool showManualButtons = true;
        private DateTime startTime;
        private ToolStripMenuItem startWDToolStripMenuItem;
        private bool stopIssued;
        private decimal tHist;
        private TextBox tb_interval;
        private TextBox tb_product;
        private TextBox tb_send;
        private TextBox tb_vendor;
        private TimeSpan timeCorrection = new TimeSpan(0L);
        private Timer timer_heartbeat;
        private Timer timer_timeout;
        private ToolTip toolTip1;
        private ToolStripMenuItem updateToolStripMenuItem;
        private UsbHidPort usb;
        private ToolStripMenuItem wDAutoStartToolStripMenuItem;
        private DateTime wmrTime;
        private string yy;

        public Wmr200DataLogger()
        {
            InitializeComponent();
            ProdVersion = Application.ProductVersion.Substring(0, 8);
            Text = "WMR200 Data Logger Sniffer v" + ProdVersion;
            autoStartWD = Convert.ToBoolean(Application.UserAppDataRegistry.GetValue("autoStartWD", false).ToString());
            wDAutoStartToolStripMenuItem.CheckOnClick = autoStartWD;
            rcv_data[0] = 0;
            timer_timeout.Interval = WMR_timeout*0x3e8;
            tb_interval.Text = heartbeat_interval.ToString();
            if (showDataGrid)
            {
                base.Width = 900;
            }
            else if (showManualButtons)
            {
                base.Width = 570;
                gb_manual.Visible = showManualButtons;
            }
            else
            {
                base.Width = 480;
            }
            autoStart = Environment.CommandLine.Contains("autostart");
            if (autoStart)
            {
                cmd_connect();
            }
        }

        private string arrayToHex(byte[] aIn)
        {
            if (IsTemperatureSensor(aIn))
            {
                return DecodeTemperatureSensor(aIn);
            }

            var str = "";
            for (var i = 1; i <= aIn[0]; i++)
            {
                str = str + aIn[i].ToString("X2") + " ";
            }
            return str.Trim();
        }

        private static bool IsTemperatureSensor(byte[] aIn)
        {
            return GetPacketId(aIn) == "D7";
        }

        private static string GetPacketId(byte[] aIn)
        {
            return aIn[1].ToString("X2");
        }

        private static string GetData(byte aIn)
        {
            return aIn.ToString();
        }

        private string DecodeTemperatureSensor(byte[] aIn)
        {
            var sensortReport = new SensorReport(aIn);

            Serialize(aIn);

            return sensortReport.ToString();
        }

        /// <summary>
        ///   s1, 6.7°C / 72%
        /// </summary>
        /// <param name="aIn"> </param>
        private void Serialize(byte[] aIn)
        {
            using (var file = File.Open(@"c:\temperature_1_64_74.bin", FileMode.Create, FileAccess.Write))
            {
                using (var writer = new BinaryWriter(file))
                {
                    writer.Write(aIn, 0, aIn[0]);
                }
            }
        }

        private string GetYear(byte[] aIn)
        {
            return string.Format("20{0}", GetData(aIn[7]));
        }

        private string GetMonth(byte[] aIn)
        {
            return GetData(aIn[6]);
        }

        private string GetDay(byte[] aIn)
        {
            return GetData(aIn[5]);
        }

        private string GetHours(byte[] aIn)
        {
            return GetData(aIn[4]);
        }

        private string GetMinutes(byte[] aIn)
        {
            return GetData(aIn[3]);
        }

        private void btn_about_Click(object sender, EventArgs e)
        {
            var location = base.Location;
            location.X += 12;
            location.Y += 0x20;
            new About().Show();
        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            if (
                MessageBox.Show("Are you sure you want to clear the output log?", "WARNING:", MessageBoxButtons.YesNo,
                                MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                msg(DateTime.Now + " Log cleared.");
                re_box.Focus();
                re_box.Clear();
            }
        }

        private void btn_connect_Click(object sender, EventArgs e)
        {
            cmd_connect();
        }

        private void btn_copy_all_Click(object sender, EventArgs e)
        {
            re_box.Focus();
            re_box.SelectAll();
            re_box.Copy();
        }

        private void btn_copy_Click(object sender, EventArgs e)
        {
            re_box.Focus();
            re_box.Copy();
        }

        private void btn_erase_Click(object sender, EventArgs e)
        {
            cmd_erase();
        }

        private void btn_reset_Click(object sender, EventArgs e)
        {
            cmd_reset();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            var now = new DateTime();
            now = DateTime.Now;
            var str = now.Year.ToString();
            var str2 = now.Month.ToString().PadLeft(2, '0');
            var str3 = now.Day.ToString().PadLeft(2, '0');
            var str4 = now.Hour.ToString().PadLeft(2, '0');
            var str5 = now.Minute.ToString().PadLeft(2, '0');
            dlg_save.FileName = "WMR200-LOG " + str + "-" + str2 + "-" + str3 + " @ " + str4 + "h" + str5 + ".rtf";
            dlg_save.ShowDialog();
        }

        private void btn_send_Click(object sender, EventArgs e)
        {
            cmd_send();
        }

        private void btn_sendD0_Click(object sender, EventArgs e)
        {
            cmd_sendD0();
        }

        private void btn_sendDA_Click(object sender, EventArgs e)
        {
            cmd_sendDA();
        }

        private void btn_sendDF_Click(object sender, EventArgs e)
        {
            cmd_stop();
        }

        private void btn_start_Click(object sender, EventArgs e)
        {
            cmd_start();
        }

        private void btn_start_WD_Click(object sender, EventArgs e)
        {
            Process.Start(@"C:\Program Files\wdisplay\WeatherD.exe");
        }

        private void btn_stop_Click(object sender, EventArgs e)
        {
            stopIssued = true;
            btn_start.Enabled = true;
            btn_erase.Enabled = true;
            btn_reset.Enabled = true;
        }

        private void btn_update_Click(object sender, EventArgs e)
        {
            new Update().Show();
        }

        private void btn_wdexport_Click(object sender, EventArgs e)
        {
        }

        private void cb_ackLIVE_CheckedChanged(object sender, EventArgs e)
        {
            inHistory = cb_ackLIVE.Checked;
        }

        private void cb_heartbeat_CheckedChanged(object sender, EventArgs e)
        {
            if (!cb_heartbeat.Checked)
            {
                print_msg("BEAT: heartbeat stopped", true);
                timer_heartbeat.Stop();
            }
        }

        private void cb_showUSB_CheckedChanged(object sender, EventArgs e)
        {
            if (!cb_showUSB.Checked && !cb_showWMR.Checked)
            {
                cb_showWMR.Checked = true;
            }
        }

        private void cb_showWMR_CheckedChanged(object sender, EventArgs e)
        {
            if (!cb_showWMR.Checked && !cb_showUSB.Checked)
            {
                cb_showUSB.Checked = true;
            }
        }

        private bool check_CRC(byte[] wmr_data)
        {
            int index = wmr_data[2];
            ushort num2 = 0;
            var num3 = Convert.ToUInt16(((wmr_data[index]*0x100) + wmr_data[index - 1]));
            for (byte i = 1; i <= (wmr_data[2] - 2); i = (byte) (i + 1))
            {
                num2 = (ushort) (num2 + wmr_data[i]);
            }
            return (num3 == num2);
        }

        private void CheckForUpdate()
        {
            UpdateCheckInfo info = null;
            if (ApplicationDeployment.IsNetworkDeployed)
            {
                var currentDeployment = ApplicationDeployment.CurrentDeployment;
                try
                {
                    info = currentDeployment.CheckForDetailedUpdate();
                }
                catch (DeploymentDownloadException exception)
                {
                    MessageBox.Show(
                        "The new version of the application cannot be downloaded at this time. \n\nPlease check your network connection, or try again later. Error: " +
                        exception.Message);
                    return;
                }
                catch (InvalidDeploymentException exception2)
                {
                    MessageBox.Show(
                        "Cannot check for a new version of the application. The ClickOnce deployment is corrupt. Please redeploy the application and try again. Error: " +
                        exception2.Message);
                    return;
                }
                catch (InvalidOperationException exception3)
                {
                    MessageBox.Show(
                        "This application cannot be updated. It is likely not a ClickOnce application. Error: " +
                        exception3.Message);
                    return;
                }
                if (info.UpdateAvailable)
                {
                    var flag = true;
                    if (!info.IsUpdateRequired)
                    {
                        var result =
                            MessageBox.Show("An update is available. Would you like to update the application now?",
                                            "Update Available", MessageBoxButtons.OKCancel);
                        if (DialogResult.OK != result)
                        {
                            flag = false;
                        }
                    }
                    else
                    {
                        MessageBox.Show(
                            "This application has detected a mandatory update from your current version to version " +
                            info.MinimumRequiredVersion + ". The application will now install the update and restart.",
                            "Update Available", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                    if (flag)
                    {
                        new Update().Show();
                    }
                }
            }
        }

        private void cmd_connect()
        {
            try
            {
                usb.ProductId = int.Parse(tb_product.Text, NumberStyles.HexNumber);
                usb.VendorId = int.Parse(tb_vendor.Text, NumberStyles.HexNumber);
                usb.CheckDevicePresent();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
        }

        private void cmd_erase()
        {
            heartbeat_stop();
            if (
                MessageBox.Show(
                    "You are about to loose all your weather history.\nAre you sure you want to erase the WMR200's Data Logger?",
                    "WARNING:", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) ==
                DialogResult.Yes)
            {
                if (btnConnect.Enabled)
                {
                    btnConnect.PerformClick();
                }
                cmd_reset();
                send_command("01 DB", "CMD ");
                msg(DateTime.Now + " WMR200 Data Logger erased");
                stopIssued = true;
            }
        }

        private void cmd_reset()
        {
            heartbeat_stop();
            btn_start.Enabled = true;
            btn_stop.Enabled = false;
            btn_erase.Enabled = true;
            btn_sendD0.Enabled = true;
            btn_sendDA.Enabled = true;
            btn_sendDF.Enabled = true;
            btn_send.Enabled = true;
            lb_start_date.Text = "00-00-0000";
            lb_start_time.Text = "@ 00h00";
            iTotal = iLive = iJunk = iHist = iWind = iRain = iUVI = iBaro = iTemp = iStat = 0;
            tHist = 0M;
            lb_total.Text = iHist.ToString();
            lb_hist.Text = iHist.ToString();
            lb_wind.Text = iWind.ToString();
            lb_rain.Text = iRain.ToString();
            lb_uvi.Text = iUVI.ToString();
            lb_baro.Text = iBaro.ToString();
            lb_temp.Text = iTemp.ToString();
            lb_batt.Text = iStat.ToString();
            lb_rec_time.Text = "0d 0h 0m";
            lb_rec_done.Text = "0 of 0";
            lb_rec_rpm.Text = "0 recs/min";
            gauge_rpm.Value = 0f;
            gauge_rpm.Cap_Idx = 1;
            gauge_rpm.CapText = gauge_rpm.Value.ToString();
            pb_done.Value = 100;
            inHistory = cb_ackLIVE.Checked;
            gotTimeFix = false;
            stopIssued = false;
            heartbeatCount = 0;
            re_box.Clear();
            startTime = DateTime.Now;
            var str = startTime.Year.ToString();
            var str2 = startTime.Month.ToString().PadLeft(2, '0');
            var str3 = startTime.Day.ToString().PadLeft(2, '0');
            print_msg("==========================================", false);
            print_msg("WMR200 DATA LOGGER SNIFFER       v" + ProdVersion, false);
            print_msg("==========================================", false);
            print_msg("                   " + str + "-" + str2 + "-" + str3, true);
            print_msg("------------------------------------------", false);
            send_command("20 00 08 01 00 00 00 00", "REST");
            print_msg("--------------------- --------------------", false);
        }

        private void cmd_send()
        {
            heartbeat_start();
            send_command(tb_send.Text, "SEND");
            stopIssued = false;
            btn_start.Enabled = false;
            btn_stop.Enabled = true;
            btn_erase.Enabled = false;
        }

        private void cmd_sendD0()
        {
            heartbeat_start();
            send_command("01 D0", "CMD ");
            stopIssued = false;
            btn_start.Enabled = false;
            btn_stop.Enabled = true;
            btn_erase.Enabled = false;
        }

        private void cmd_sendDA()
        {
            heartbeat_start();
            send_command("01 DA", "CMD ");
            stopIssued = false;
            btn_start.Enabled = false;
            btn_stop.Enabled = true;
            btn_erase.Enabled = false;
        }

        private void cmd_start()
        {
            if (btnConnect.Enabled)
            {
                autoStart = true;
                cmd_connect();
            }
            else
            {
                cmd_reset();
                cmd_sendD0();
            }
            stopIssued = false;
        }

        private void cmd_stop()
        {
            heartbeat_stop();
            send_command("01 DF", "CMD ");
            stopIssued = true;
            btn_start.Enabled = true;
            btn_erase.Enabled = true;
            btn_reset.Enabled = true;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void dlg_save_FileOk(object sender, CancelEventArgs e)
        {
            re_box.SaveFile(dlg_save.FileName);
        }

        private void fix_pkt_timestamp(ref byte[] wmr_data)
        {
            if (wmr_data[1] != 0xd9)
            {
                if (iHist < 2)
                {
                    print_msg(
                        "TIME: Was 20" + wmr_data[7].ToString() + "-" + wmr_data[6].ToString().PadLeft(2, '0') + "-" +
                        wmr_data[5].ToString().PadLeft(2, '0') + " " + wmr_data[4].ToString().PadLeft(2, '0') + ":" +
                        wmr_data[3].ToString().PadLeft(2, '0'), true);
                }
                var time = new DateTime(wmr_data[7] + 0x7d0, wmr_data[6], wmr_data[5], wmr_data[4], wmr_data[3], 0);
                time = time.AddMinutes(((long) histTimeFix));
                wmr_data[3] = Convert.ToByte(time.Minute);
                wmr_data[4] = Convert.ToByte(time.Hour);
                wmr_data[5] = Convert.ToByte(time.Day);
                wmr_data[6] = Convert.ToByte(time.Month);
                wmr_data[7] = Convert.ToByte((time.Year - 0x7d0));
            }
        }

        private void heartbeat_reset()
        {
            timer_heartbeat.Stop();
            timer_heartbeat.Start();
        }

        private void heartbeat_start()
        {
            if (cb_heartbeat.Checked)
            {
                print_msg("BEAT: heartbeat started [D0]", true);
                timer_heartbeat.Interval = heartbeat_interval*0x3e8;
                timer_heartbeat.Start();
            }
        }

        private void heartbeat_stop()
        {
            if (cb_heartbeat.Checked)
            {
                print_msg("BEAT: heartbeat stopped", true);
                timer_heartbeat.Stop();
            }
        }

        private void InitializeComponent()
        {
            components = new Container();
            var manager = new ComponentResourceManager(typeof (Wmr200DataLogger));
            btnClear = new Button();
            btnConnect = new Button();
            btn_copy = new Button();
            btn_copy_all = new Button();
            btn_reset = new Button();
            btn_save = new Button();
            btn_send = new Button();
            btn_sendD0 = new Button();
            btn_sendDA = new Button();
            btn_sendDF = new Button();
            btn_wdexport = new Button();
            cb_scroll = new CheckBox();
            cb_showUSB = new CheckBox();
            cb_showWMR = new CheckBox();
            dg_history = new DataGridView();
            pkt_day = new DataGridViewTextBoxColumn();
            pkt_mth = new DataGridViewTextBoxColumn();
            pkt_year = new DataGridViewTextBoxColumn();
            pkt_hour = new DataGridViewTextBoxColumn();
            pkt_min = new DataGridViewTextBoxColumn();
            pkt_temp = new DataGridViewTextBoxColumn();
            pkt_hum = new DataGridViewTextBoxColumn();
            pkt_dewp = new DataGridViewTextBoxColumn();
            pkt_baro = new DataGridViewTextBoxColumn();
            pkt_windSpd = new DataGridViewTextBoxColumn();
            pkt_windGst = new DataGridViewTextBoxColumn();
            pkt_windDir = new DataGridViewTextBoxColumn();
            pkt_rainMin = new DataGridViewTextBoxColumn();
            pkt_rainDay = new DataGridViewTextBoxColumn();
            pkt_rainMth = new DataGridViewTextBoxColumn();
            pkt_rainYear = new DataGridViewTextBoxColumn();
            pkt_hindex = new DataGridViewTextBoxColumn();
            dlg_save = new SaveFileDialog();
            gb_counts = new GroupBox();
            lb_total_h = new Label();
            lb_total = new Label();
            lb_batt = new Label();
            lb_batt_h = new Label();
            lb_temp = new Label();
            lb_temp_h = new Label();
            lb_baro = new Label();
            lb_baro_h = new Label();
            lb_uvi = new Label();
            lb_uvi_h = new Label();
            lb_rain = new Label();
            lb_rain_h = new Label();
            lb_wind = new Label();
            lb_wind_h = new Label();
            lb_hist = new Label();
            lb_hist_h = new Label();
            gb_manual = new GroupBox();
            lb_seconds_h = new Label();
            tb_interval = new TextBox();
            cb_heartbeat = new CheckBox();
            cb_ackLIVE = new CheckBox();
            rb_ackNONE = new RadioButton();
            rb_ackDA = new RadioButton();
            tb_vendor = new TextBox();
            tb_product = new TextBox();
            lb_product = new Label();
            lb_vendor = new Label();
            tb_send = new TextBox();
            gb_usb_data = new GroupBox();
            lb_missing = new Label();
            lb_junk = new Label();
            lb_rec_time = new Label();
            lb_corrupt = new Label();
            lb_cur_date = new Label();
            lb_cur_h = new Label();
            lb_cur_time = new Label();
            lb_done = new Label();
            lb_end_date = new Label();
            lb_end_h = new Label();
            lb_end_time = new Label();
            lb_heartbeatCount = new Label();
            lb_message = new ListBox();
            lb_missing_h = new Label();
            lb_percent = new Label();
            lb_rec_done = new Label();
            lb_rec_rpm = new Label();
            lb_records_h = new Label();
            lb_start_date = new Label();
            lb_start_h = new Label();
            lb_start_time = new Label();
            lb_timeLeft = new Label();
            pb_done = new ProgressBar();
            re_box = new RichTextBox();
            gb_wx_data = new GroupBox();
            timer_heartbeat = new Timer(components);
            timer_timeout = new Timer(components);
            toolTip1 = new ToolTip(components);
            btn_start = new Button();
            btn_stop = new Button();
            btn_erase = new Button();
            gb_dataLogger = new GroupBox();
            rb_DL10 = new RadioButton();
            rb_DL5 = new RadioButton();
            rb_DL2 = new RadioButton();
            rb_DL1 = new RadioButton();
            errorProvider1 = new ErrorProvider(components);
            menuStrip1 = new MenuStrip();
            fileToolStripMenuItem = new ToolStripMenuItem();
            quitToolStripMenuItem = new ToolStripMenuItem();
            updateToolStripMenuItem = new ToolStripMenuItem();
            aboutToolStripMenuItem = new ToolStripMenuItem();
            configToolStripMenuItem = new ToolStripMenuItem();
            wDAutoStartToolStripMenuItem = new ToolStripMenuItem();
            startWDToolStripMenuItem = new ToolStripMenuItem();
            pictureBox1 = new PictureBox();
            usb = new UsbHidPort(components);
            gauge_rpm = new AGauge();
            ((ISupportInitialize) dg_history).BeginInit();
            gb_counts.SuspendLayout();
            gb_manual.SuspendLayout();
            gb_usb_data.SuspendLayout();
            gb_wx_data.SuspendLayout();
            gb_dataLogger.SuspendLayout();
            ((ISupportInitialize) errorProvider1).BeginInit();
            menuStrip1.SuspendLayout();
            ((ISupportInitialize) pictureBox1).BeginInit();
            base.SuspendLayout();
            btnClear.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            btnClear.Cursor = Cursors.Hand;
            btnClear.ForeColor = Color.Chocolate;
            btnClear.Location = new Point(0x177, 360);
            btnClear.Name = "btnClear";
            btnClear.Size = new Size(0x4b, 0x17);
            btnClear.TabIndex = 7;
            btnClear.Text = "Clear";
            toolTip1.SetToolTip(btnClear, "Clear output text");
            btnClear.UseVisualStyleBackColor = true;
            btnClear.Click += btn_clear_Click;
            btnConnect.AutoSize = true;
            btnConnect.Cursor = Cursors.Hand;
            btnConnect.ForeColor = Color.Black;
            btnConnect.Location = new Point(4, 0x60);
            btnConnect.Name = "btnConnect";
            btnConnect.Size = new Size(0x4c, 0x17);
            btnConnect.TabIndex = 1;
            btnConnect.Tag = "CNCT";
            btnConnect.Text = "CONNECT";
            toolTip1.SetToolTip(btnConnect, "Connect to the WMR200 Weather Station");
            btnConnect.UseVisualStyleBackColor = true;
            btnConnect.Click += btn_connect_Click;
            btn_copy.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            btn_copy.Cursor = Cursors.Hand;
            btn_copy.Location = new Point(0x177, 0x180);
            btn_copy.Name = "btn_copy";
            btn_copy.Size = new Size(0x4b, 0x17);
            btn_copy.TabIndex = 0x17;
            btn_copy.Text = "Copy";
            toolTip1.SetToolTip(btn_copy, "Copy selected output text to clipboard");
            btn_copy.UseVisualStyleBackColor = true;
            btn_copy.Click += btn_copy_Click;
            btn_copy_all.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            btn_copy_all.Cursor = Cursors.Hand;
            btn_copy_all.Location = new Point(0x177, 0x198);
            btn_copy_all.Name = "btn_copy_all";
            btn_copy_all.Size = new Size(0x4b, 0x17);
            btn_copy_all.TabIndex = 6;
            btn_copy_all.Tag = "all";
            btn_copy_all.Text = "Copy All";
            toolTip1.SetToolTip(btn_copy_all, "Copy output text to clipboard");
            btn_copy_all.UseVisualStyleBackColor = true;
            btn_copy_all.Click += btn_copy_all_Click;
            btn_reset.AccessibleDescription = "Send RESET to WMR200";
            btn_reset.AccessibleName = "RESET BUUTON";
            btn_reset.AutoSize = true;
            btn_reset.Cursor = Cursors.Hand;
            btn_reset.DialogResult = DialogResult.Cancel;
            btn_reset.Enabled = false;
            btn_reset.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            btn_reset.ForeColor = Color.Black;
            btn_reset.Location = new Point(4, 0x7c);
            btn_reset.Name = "btn_reset";
            btn_reset.Size = new Size(0x4b, 0x17);
            btn_reset.TabIndex = 2;
            btn_reset.Tag = "REST";
            btn_reset.Text = "RESET";
            toolTip1.SetToolTip(btn_reset, "Reset the USB Port on the WMR200");
            btn_reset.UseVisualStyleBackColor = true;
            btn_reset.Click += btn_reset_Click;
            btn_save.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            btn_save.Cursor = Cursors.Hand;
            btn_save.DialogResult = DialogResult.OK;
            btn_save.Location = new Point(0x177, 0x1b0);
            btn_save.Name = "btn_save";
            btn_save.Size = new Size(0x4b, 0x17);
            btn_save.TabIndex = 5;
            btn_save.Text = "Save";
            toolTip1.SetToolTip(btn_save, "Save the log to a .rtf file");
            btn_save.UseVisualStyleBackColor = true;
            btn_save.Click += btn_save_Click;
            btn_send.AutoSize = true;
            btn_send.Cursor = Cursors.Hand;
            btn_send.Enabled = false;
            btn_send.ForeColor = Color.Indigo;
            btn_send.Location = new Point(4, 0x106);
            btn_send.Name = "btn_send";
            btn_send.Size = new Size(0x4b, 0x17);
            btn_send.TabIndex = 0x15;
            btn_send.Tag = "SEND";
            btn_send.Text = "Send";
            toolTip1.SetToolTip(btn_send,
                                "TEST:\r\nSend the command above\r\nto the WMR200 for testing\r\nother protocol posibilities.");
            btn_send.UseVisualStyleBackColor = true;
            btn_send.Click += btn_send_Click;
            btn_sendD0.AutoSize = true;
            btn_sendD0.Cursor = Cursors.Hand;
            btn_sendD0.Enabled = false;
            btn_sendD0.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            btn_sendD0.ForeColor = Color.Blue;
            btn_sendD0.Location = new Point(4, 0x98);
            btn_sendD0.Name = "btn_sendD0";
            btn_sendD0.Size = new Size(0x4b, 0x17);
            btn_sendD0.TabIndex = 3;
            btn_sendD0.Tag = "BUTN";
            btn_sendD0.Text = "01 D0";
            toolTip1.SetToolTip(btn_sendD0, "Start Heartbeat");
            btn_sendD0.UseVisualStyleBackColor = true;
            btn_sendD0.Click += btn_sendD0_Click;
            btn_sendDA.AutoSize = true;
            btn_sendDA.Cursor = Cursors.Hand;
            btn_sendDA.Enabled = false;
            btn_sendDA.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            btn_sendDA.ForeColor = Color.Green;
            btn_sendDA.Location = new Point(4, 180);
            btn_sendDA.Name = "btn_sendDA";
            btn_sendDA.Size = new Size(0x4b, 0x17);
            btn_sendDA.TabIndex = 12;
            btn_sendDA.Tag = "BUTN";
            btn_sendDA.Text = "01 DA";
            toolTip1.SetToolTip(btn_sendDA, "Request more history data");
            btn_sendDA.UseVisualStyleBackColor = true;
            btn_sendDA.Click += btn_sendDA_Click;
            btn_sendDF.AutoSize = true;
            btn_sendDF.Cursor = Cursors.Hand;
            btn_sendDF.Enabled = false;
            btn_sendDF.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            btn_sendDF.ForeColor = Color.Red;
            btn_sendDF.Location = new Point(4, 0xd0);
            btn_sendDF.Name = "btn_sendDF";
            btn_sendDF.Size = new Size(0x4b, 0x17);
            btn_sendDF.TabIndex = 4;
            btn_sendDF.Tag = "BUTN";
            btn_sendDF.Text = "01 DF";
            toolTip1.SetToolTip(btn_sendDF, "STOP:\r\nStop communication and\r\nenable Data Logging.");
            btn_sendDF.UseVisualStyleBackColor = true;
            btn_sendDF.Click += btn_sendDF_Click;
            btn_wdexport.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
            btn_wdexport.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            btn_wdexport.Location = new Point(0x22f, 0x290);
            btn_wdexport.Name = "btn_wdexport";
            btn_wdexport.Size = new Size(0x91, 0x17);
            btn_wdexport.TabIndex = 8;
            btn_wdexport.Text = "Export to WD";
            toolTip1.SetToolTip(btn_wdexport, "Eather Display Export");
            btn_wdexport.UseVisualStyleBackColor = true;
            btn_wdexport.Click += btn_wdexport_Click;
            cb_scroll.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            cb_scroll.AutoSize = true;
            cb_scroll.Checked = true;
            cb_scroll.CheckState = CheckState.Checked;
            cb_scroll.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            cb_scroll.Location = new Point(0x129, 0x1cd);
            cb_scroll.Name = "cb_scroll";
            cb_scroll.RightToLeft = RightToLeft.Yes;
            cb_scroll.Size = new Size(0x48, 0x11);
            cb_scroll.TabIndex = 0x1a;
            cb_scroll.Text = "Scroll text";
            cb_scroll.UseVisualStyleBackColor = true;
            cb_showUSB.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            cb_showUSB.AutoSize = true;
            cb_showUSB.Checked = true;
            cb_showUSB.CheckState = CheckState.Checked;
            cb_showUSB.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            cb_showUSB.Location = new Point(6, 460);
            cb_showUSB.Name = "cb_showUSB";
            cb_showUSB.Size = new Size(0x66, 0x11);
            cb_showUSB.TabIndex = 0x26;
            cb_showUSB.Text = "Show USB data";
            toolTip1.SetToolTip(cb_showUSB, "Show USB data sent by WMR200 Console");
            cb_showUSB.UseVisualStyleBackColor = true;
            cb_showUSB.CheckedChanged += cb_showUSB_CheckedChanged;
            cb_showWMR.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            cb_showWMR.AutoSize = true;
            cb_showWMR.Checked = true;
            cb_showWMR.CheckState = CheckState.Checked;
            cb_showWMR.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            cb_showWMR.Location = new Point(120, 460);
            cb_showWMR.Name = "cb_showWMR";
            cb_showWMR.Size = new Size(0x6c, 0x11);
            cb_showWMR.TabIndex = 0x1c;
            cb_showWMR.Text = "Show WMR data";
            toolTip1.SetToolTip(cb_showWMR, "Show and log only history records");
            cb_showWMR.UseVisualStyleBackColor = true;
            cb_showWMR.CheckedChanged += cb_showWMR_CheckedChanged;
            dg_history.BackgroundColor = Color.Silver;
            dg_history.BorderStyle = BorderStyle.None;
            dg_history.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            dg_history.Columns.AddRange(new DataGridViewColumn[]
                {
                    pkt_day, pkt_mth, pkt_year, pkt_hour, pkt_min, pkt_temp, pkt_hum, pkt_dewp, pkt_baro, pkt_windSpd,
                    pkt_windGst, pkt_windDir, pkt_rainMin, pkt_rainDay, pkt_rainMth, pkt_rainYear,
                    pkt_hindex
                });
            dg_history.Dock = DockStyle.Fill;
            dg_history.Location = new Point(3, 0x10);
            dg_history.Margin = new Padding(0);
            dg_history.Name = "dg_history";
            dg_history.Size = new Size(0x1a0, 630);
            dg_history.TabIndex = 0;
            pkt_day.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            pkt_day.HeaderText = "Day";
            pkt_day.Name = "pkt_day";
            pkt_day.Width = 0x36;
            pkt_mth.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            pkt_mth.HeaderText = "Mth";
            pkt_mth.Name = "pkt_mth";
            pkt_mth.Width = 0x35;
            pkt_year.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            pkt_year.HeaderText = "Year";
            pkt_year.Name = "pkt_year";
            pkt_year.ReadOnly = true;
            pkt_year.Width = 0x3a;
            pkt_hour.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            pkt_hour.HeaderText = "Hour";
            pkt_hour.Name = "pkt_hour";
            pkt_hour.ReadOnly = true;
            pkt_hour.Width = 0x3b;
            pkt_min.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            pkt_min.HeaderText = "Min";
            pkt_min.Name = "pkt_min";
            pkt_min.ReadOnly = true;
            pkt_min.Width = 0x34;
            pkt_temp.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            pkt_temp.HeaderText = "Temp";
            pkt_temp.Name = "pkt_temp";
            pkt_temp.ReadOnly = true;
            pkt_temp.Width = 0x3f;
            pkt_hum.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            pkt_hum.HeaderText = "Hum";
            pkt_hum.Name = "pkt_hum";
            pkt_hum.ReadOnly = true;
            pkt_hum.Width = 0x39;
            pkt_dewp.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            pkt_dewp.HeaderText = "Dewp";
            pkt_dewp.Name = "pkt_dewp";
            pkt_dewp.ReadOnly = true;
            pkt_dewp.Width = 0x40;
            pkt_baro.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            pkt_baro.HeaderText = "Baro";
            pkt_baro.Name = "pkt_baro";
            pkt_baro.ReadOnly = true;
            pkt_baro.Width = 0x3a;
            pkt_windSpd.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            pkt_windSpd.HeaderText = "Wind Spd";
            pkt_windSpd.Name = "pkt_windSpd";
            pkt_windSpd.ReadOnly = true;
            pkt_windSpd.Width = 0x57;
            pkt_windGst.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            pkt_windGst.HeaderText = "Wind Gst";
            pkt_windGst.Name = "pkt_windGst";
            pkt_windGst.ReadOnly = true;
            pkt_windGst.Width = 0x54;
            pkt_windDir.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            pkt_windDir.HeaderText = "Wind Dir";
            pkt_windDir.Name = "pkt_windDir";
            pkt_windDir.ReadOnly = true;
            pkt_windDir.Width = 0x51;
            pkt_rainMin.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            pkt_rainMin.HeaderText = "Rain Min";
            pkt_rainMin.Name = "pkt_rainMin";
            pkt_rainMin.ReadOnly = true;
            pkt_rainMin.Width = 0x52;
            pkt_rainDay.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            pkt_rainDay.HeaderText = "Rain Day";
            pkt_rainDay.Name = "pkt_rainDay";
            pkt_rainDay.ReadOnly = true;
            pkt_rainDay.Width = 0x54;
            pkt_rainMth.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            pkt_rainMth.HeaderText = "Rain Mth";
            pkt_rainMth.Name = "pkt_rainMth";
            pkt_rainMth.ReadOnly = true;
            pkt_rainMth.Width = 0x53;
            pkt_rainYear.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            pkt_rainYear.HeaderText = "Rain Year";
            pkt_rainYear.Name = "pkt_rainYear";
            pkt_rainYear.ReadOnly = true;
            pkt_rainYear.Width = 0x58;
            pkt_hindex.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            pkt_hindex.HeaderText = "HIndex";
            pkt_hindex.Name = "pkt_hindex";
            pkt_hindex.ReadOnly = true;
            pkt_hindex.Width = 0x48;
            dlg_save.Title = "Save Logfile";
            dlg_save.FileOk += dlg_save_FileOk;
            gb_counts.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            gb_counts.Controls.Add(lb_total_h);
            gb_counts.Controls.Add(lb_total);
            gb_counts.Controls.Add(lb_batt);
            gb_counts.Controls.Add(lb_batt_h);
            gb_counts.Controls.Add(lb_temp);
            gb_counts.Controls.Add(lb_temp_h);
            gb_counts.Controls.Add(lb_baro);
            gb_counts.Controls.Add(lb_baro_h);
            gb_counts.Controls.Add(lb_uvi);
            gb_counts.Controls.Add(lb_uvi_h);
            gb_counts.Controls.Add(lb_rain);
            gb_counts.Controls.Add(lb_rain_h);
            gb_counts.Controls.Add(lb_wind);
            gb_counts.Controls.Add(lb_wind_h);
            gb_counts.Controls.Add(lb_hist);
            gb_counts.Controls.Add(lb_hist_h);
            gb_counts.ForeColor = Color.Black;
            gb_counts.Location = new Point(0x177, 0x4d);
            gb_counts.Name = "gb_counts";
            gb_counts.Size = new Size(0x4d, 0x117);
            gb_counts.TabIndex = 0x1b;
            gb_counts.TabStop = false;
            toolTip1.SetToolTip(gb_counts, "WMR200 Data packet counts");
            lb_total_h.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            lb_total_h.ForeColor = Color.Black;
            lb_total_h.Location = new Point(3, 10);
            lb_total_h.Name = "lb_total_h";
            lb_total_h.Size = new Size(70, 0x13);
            lb_total_h.TabIndex = 0x38;
            lb_total_h.Text = "TOTAL";
            lb_total_h.TextAlign = ContentAlignment.MiddleCenter;
            toolTip1.SetToolTip(lb_total_h, "Total records downloaded\r\nincluding live and history.\r\n");
            lb_total.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            lb_total.ForeColor = Color.Black;
            lb_total.Location = new Point(3, 0x1c);
            lb_total.Name = "lb_total";
            lb_total.Size = new Size(70, 0x13);
            lb_total.TabIndex = 0x37;
            lb_total.Text = "0";
            lb_total.TextAlign = ContentAlignment.MiddleCenter;
            toolTip1.SetToolTip(lb_total, "Total records downloaded\r\nincluding live and history.");
            lb_batt.ForeColor = Color.DimGray;
            lb_batt.Location = new Point(3, 0x102);
            lb_batt.Name = "lb_batt";
            lb_batt.Size = new Size(70, 0x13);
            lb_batt.TabIndex = 0x36;
            lb_batt.Text = "0";
            lb_batt.TextAlign = ContentAlignment.MiddleCenter;
            toolTip1.SetToolTip(lb_batt, "STATUS:\r\nBattery, RF Signal and \r\nSensor loss status.");
            lb_batt_h.AutoSize = true;
            lb_batt_h.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_batt_h.ForeColor = Color.DimGray;
            lb_batt_h.Location = new Point(5, 0xf5);
            lb_batt_h.Name = "lb_batt_h";
            lb_batt_h.Size = new Size(0x44, 13);
            lb_batt_h.TabIndex = 0x35;
            lb_batt_h.Text = "Status Count";
            toolTip1.SetToolTip(lb_batt_h, "STATUS:\r\nBattery, RF Signal and \r\nSensor loss status.");
            lb_temp.ForeColor = Color.Red;
            lb_temp.Location = new Point(3, 0xe2);
            lb_temp.Name = "lb_temp";
            lb_temp.Size = new Size(70, 0x13);
            lb_temp.TabIndex = 0x34;
            lb_temp.Text = "0";
            lb_temp.TextAlign = ContentAlignment.MiddleCenter;
            toolTip1.SetToolTip(lb_temp, "Number of temperature \r\nand humidity updates.");
            lb_temp_h.AutoSize = true;
            lb_temp_h.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_temp_h.ForeColor = Color.Red;
            lb_temp_h.Location = new Point(6, 0xd5);
            lb_temp_h.Name = "lb_temp_h";
            lb_temp_h.Size = new Size(0x41, 13);
            lb_temp_h.TabIndex = 0x33;
            lb_temp_h.Text = "Temp Count";
            toolTip1.SetToolTip(lb_temp_h, "Number of temperature \r\nand humidity updates.");
            lb_baro.ForeColor = Color.Fuchsia;
            lb_baro.Location = new Point(3, 0xc2);
            lb_baro.Name = "lb_baro";
            lb_baro.Size = new Size(70, 0x13);
            lb_baro.TabIndex = 50;
            lb_baro.Text = "0";
            lb_baro.TextAlign = ContentAlignment.MiddleCenter;
            toolTip1.SetToolTip(lb_baro, "Number of pressure updates.");
            lb_baro_h.AutoSize = true;
            lb_baro_h.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_baro_h.ForeColor = Color.Fuchsia;
            lb_baro_h.Location = new Point(9, 0xb5);
            lb_baro_h.Name = "lb_baro_h";
            lb_baro_h.Size = new Size(60, 13);
            lb_baro_h.TabIndex = 0x31;
            lb_baro_h.Text = "Baro Count";
            toolTip1.SetToolTip(lb_baro_h, "Number of pressure updates.");
            lb_uvi.ForeColor = Color.DarkOrange;
            lb_uvi.Location = new Point(3, 0xa2);
            lb_uvi.Name = "lb_uvi";
            lb_uvi.Size = new Size(70, 0x13);
            lb_uvi.TabIndex = 0x30;
            lb_uvi.Text = "0";
            lb_uvi.TextAlign = ContentAlignment.MiddleCenter;
            toolTip1.SetToolTip(lb_uvi, "Number of UV sensor updates.");
            lb_uvi_h.AutoSize = true;
            lb_uvi_h.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_uvi_h.ForeColor = Color.DarkOrange;
            lb_uvi_h.Location = new Point(11, 0x95);
            lb_uvi_h.Name = "lb_uvi_h";
            lb_uvi_h.Size = new Size(0x38, 13);
            lb_uvi_h.TabIndex = 0x2f;
            lb_uvi_h.Text = "UVI Count";
            toolTip1.SetToolTip(lb_uvi_h, "Number of UV sensor updates.");
            lb_rain.ForeColor = Color.Blue;
            lb_rain.Location = new Point(3, 130);
            lb_rain.Name = "lb_rain";
            lb_rain.Size = new Size(70, 0x13);
            lb_rain.TabIndex = 0x2e;
            lb_rain.Text = "0";
            lb_rain.TextAlign = ContentAlignment.MiddleCenter;
            toolTip1.SetToolTip(lb_rain, "Number of rain meter updates.");
            lb_rain_h.AutoSize = true;
            lb_rain_h.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_rain_h.ForeColor = Color.Blue;
            lb_rain_h.Location = new Point(8, 0x75);
            lb_rain_h.Name = "lb_rain_h";
            lb_rain_h.Size = new Size(60, 13);
            lb_rain_h.TabIndex = 0x2d;
            lb_rain_h.Text = "Rain Count";
            toolTip1.SetToolTip(lb_rain_h, "Number of rain meter updates.");
            lb_wind.ForeColor = Color.Green;
            lb_wind.Location = new Point(3, 0x62);
            lb_wind.Name = "lb_wind";
            lb_wind.Size = new Size(70, 0x13);
            lb_wind.TabIndex = 0x2c;
            lb_wind.Text = "0";
            lb_wind.TextAlign = ContentAlignment.MiddleCenter;
            toolTip1.SetToolTip(lb_wind, "Number of anemometer updates.");
            lb_wind_h.AutoSize = true;
            lb_wind_h.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_wind_h.ForeColor = Color.Green;
            lb_wind_h.Location = new Point(6, 0x55);
            lb_wind_h.Name = "lb_wind_h";
            lb_wind_h.Size = new Size(0x3f, 13);
            lb_wind_h.TabIndex = 0x2b;
            lb_wind_h.Text = "Wind Count";
            toolTip1.SetToolTip(lb_wind_h, "Number of anemometer updates.");
            lb_hist.ForeColor = Color.Maroon;
            lb_hist.Location = new Point(3, 0x42);
            lb_hist.Name = "lb_hist";
            lb_hist.Size = new Size(70, 0x13);
            lb_hist.TabIndex = 0x2a;
            lb_hist.Text = "0";
            lb_hist.TextAlign = ContentAlignment.MiddleCenter;
            toolTip1.SetToolTip(lb_hist, "Number of history\r\nrecords received.");
            lb_hist_h.AutoSize = true;
            lb_hist_h.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_hist_h.ForeColor = Color.Maroon;
            lb_hist_h.Location = new Point(3, 0x35);
            lb_hist_h.Name = "lb_hist_h";
            lb_hist_h.Size = new Size(70, 13);
            lb_hist_h.TabIndex = 0x29;
            lb_hist_h.Text = "History Count";
            toolTip1.SetToolTip(lb_hist_h, "Number of history\r\nrecords received.");
            gb_manual.Controls.Add(lb_seconds_h);
            gb_manual.Controls.Add(tb_interval);
            gb_manual.Controls.Add(cb_heartbeat);
            gb_manual.Controls.Add(cb_ackLIVE);
            gb_manual.Controls.Add(rb_ackNONE);
            gb_manual.Controls.Add(rb_ackDA);
            gb_manual.Controls.Add(tb_vendor);
            gb_manual.Controls.Add(tb_product);
            gb_manual.Controls.Add(lb_product);
            gb_manual.Controls.Add(lb_vendor);
            gb_manual.Controls.Add(btn_reset);
            gb_manual.Controls.Add(btnConnect);
            gb_manual.Controls.Add(btn_sendDA);
            gb_manual.Controls.Add(btn_sendDF);
            gb_manual.Controls.Add(btn_sendD0);
            gb_manual.Controls.Add(btn_send);
            gb_manual.Controls.Add(tb_send);
            gb_manual.Location = new Point(0x1d2, 0x1f);
            gb_manual.Name = "gb_manual";
            gb_manual.Size = new Size(0x54, 0x1f7);
            gb_manual.TabIndex = 100;
            gb_manual.TabStop = false;
            gb_manual.Text = "Manual";
            lb_seconds_h.AutoSize = true;
            lb_seconds_h.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_seconds_h.Location = new Point(0x1d, 0x1cd);
            lb_seconds_h.Name = "lb_seconds_h";
            lb_seconds_h.Size = new Size(0x2f, 13);
            lb_seconds_h.TabIndex = 0x6c;
            lb_seconds_h.Text = "seconds";
            toolTip1.SetToolTip(lb_seconds_h,
                                "HEARTBEAT:\r\nSet number of seconds for the heartbeat.\r\nThis must be less than 30 seconds as the\r\nWMR200 times out after that and stops \r\nsending data and goes into history data \r\nlogging mode.");
            tb_interval.Location = new Point(4, 0x1ca);
            tb_interval.Name = "tb_interval";
            tb_interval.Size = new Size(0x19, 20);
            tb_interval.TabIndex = 0x6d;
            tb_interval.Tag = "";
            tb_interval.Text = "30";
            tb_interval.TextAlign = HorizontalAlignment.Center;
            toolTip1.SetToolTip(tb_interval,
                                "HEARTBEAT:\r\nSet number of seconds for the heartbeat.\r\nThis must be less than 30 seconds as the\r\nWMR200 times out after that and stops \r\nsending data and goes into history data \r\nlogging mode.");
            tb_interval.Click += tb_interval_TextChanged;
            cb_heartbeat.AutoSize = true;
            cb_heartbeat.Checked = true;
            cb_heartbeat.CheckState = CheckState.Checked;
            cb_heartbeat.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            cb_heartbeat.Location = new Point(4, 0x1b7);
            cb_heartbeat.Name = "cb_heartbeat";
            cb_heartbeat.Size = new Size(0x49, 0x11);
            cb_heartbeat.TabIndex = 0x6b;
            cb_heartbeat.Text = "Heartbeat";
            toolTip1.SetToolTip(cb_heartbeat,
                                "HEARTBEAT:\r\nSet number of seconds for the heartbeat.\r\nThis must be less than 30 seconds as the\r\nWMR200 times out after that and stops \r\nsending data and goes into history data \r\nlogging mode.");
            cb_heartbeat.UseVisualStyleBackColor = true;
            cb_heartbeat.Click += cb_heartbeat_CheckedChanged;
            cb_ackLIVE.CheckAlign = ContentAlignment.TopLeft;
            cb_ackLIVE.Checked = true;
            cb_ackLIVE.CheckState = CheckState.Checked;
            cb_ackLIVE.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            cb_ackLIVE.Location = new Point(4, 0x193);
            cb_ackLIVE.Name = "cb_ackLIVE";
            cb_ackLIVE.Size = new Size(0x4b, 0x24);
            cb_ackLIVE.TabIndex = 0x6a;
            cb_ackLIVE.Text = "Send DA for live during history";
            toolTip1.SetToolTip(cb_ackLIVE, "Send a DA after each live packet\r\nwhen in history mode.");
            cb_ackLIVE.UseVisualStyleBackColor = true;
            cb_ackLIVE.Click += cb_ackLIVE_CheckedChanged;
            rb_ackNONE.CheckAlign = ContentAlignment.TopLeft;
            rb_ackNONE.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            rb_ackNONE.Location = new Point(4, 0x126);
            rb_ackNONE.Name = "rb_ackNONE";
            rb_ackNONE.Size = new Size(0x4c, 0x31);
            rb_ackNONE.TabIndex = 0x69;
            rb_ackNONE.Text = "Send none for History ACK";
            toolTip1.SetToolTip(rb_ackNONE, "Send nothing after receiving \r\nhistory [d2] records.");
            rb_ackNONE.UseVisualStyleBackColor = true;
            rb_ackDA.CheckAlign = ContentAlignment.TopLeft;
            rb_ackDA.Checked = true;
            rb_ackDA.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            rb_ackDA.Location = new Point(4, 0x155);
            rb_ackDA.Name = "rb_ackDA";
            rb_ackDA.Size = new Size(0x4c, 0x3f);
            rb_ackDA.TabIndex = 0x68;
            rb_ackDA.TabStop = true;
            rb_ackDA.Text = "Send \"01 DA\" for History ACK";
            rb_ackDA.TextAlign = ContentAlignment.TopLeft;
            toolTip1.SetToolTip(rb_ackDA, "Send this after receiving\r\nhistory [d2] data packets");
            rb_ackDA.UseVisualStyleBackColor = true;
            tb_vendor.BackColor = SystemColors.Info;
            tb_vendor.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            tb_vendor.Location = new Point(5, 0x1f);
            tb_vendor.Name = "tb_vendor";
            tb_vendor.ReadOnly = true;
            tb_vendor.Size = new Size(0x4a, 20);
            tb_vendor.TabIndex = 0x66;
            tb_vendor.Text = "0FDE";
            tb_vendor.TextAlign = HorizontalAlignment.Center;
            tb_product.BackColor = SystemColors.Info;
            tb_product.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            tb_product.Location = new Point(5, 0x44);
            tb_product.Name = "tb_product";
            tb_product.ReadOnly = true;
            tb_product.Size = new Size(0x4a, 20);
            tb_product.TabIndex = 0x67;
            tb_product.Text = "CA01";
            tb_product.TextAlign = HorizontalAlignment.Center;
            lb_product.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_product.Location = new Point(5, 0x35);
            lb_product.Name = "lb_product";
            lb_product.Size = new Size(0x4a, 0x11);
            lb_product.TabIndex = 0x65;
            lb_product.Text = "Product ID";
            lb_product.TextAlign = ContentAlignment.TopCenter;
            lb_vendor.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_vendor.Location = new Point(5, 0x10);
            lb_vendor.Name = "lb_vendor";
            lb_vendor.Size = new Size(0x4a, 0x11);
            lb_vendor.TabIndex = 100;
            lb_vendor.Text = "Vendor ID";
            lb_vendor.TextAlign = ContentAlignment.TopCenter;
            tb_send.Location = new Point(4, 0xed);
            tb_send.Name = "tb_send";
            tb_send.Size = new Size(0x4b, 20);
            tb_send.TabIndex = 20;
            tb_send.Text = "02 D0 DA";
            tb_send.TextAlign = HorizontalAlignment.Center;
            toolTip1.SetToolTip(tb_send, "TEST:\r\nEnter WMR200 command then\r\npress the SEND button.");
            gb_usb_data.Anchor = AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            gb_usb_data.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            gb_usb_data.Controls.Add(lb_missing);
            gb_usb_data.Controls.Add(lb_junk);
            gb_usb_data.Controls.Add(lb_rec_time);
            gb_usb_data.Controls.Add(gauge_rpm);
            gb_usb_data.Controls.Add(btnClear);
            gb_usb_data.Controls.Add(btn_copy);
            gb_usb_data.Controls.Add(btn_copy_all);
            gb_usb_data.Controls.Add(btn_save);
            gb_usb_data.Controls.Add(gb_counts);
            gb_usb_data.Controls.Add(cb_scroll);
            gb_usb_data.Controls.Add(cb_showUSB);
            gb_usb_data.Controls.Add(cb_showWMR);
            gb_usb_data.Controls.Add(lb_corrupt);
            gb_usb_data.Controls.Add(lb_cur_date);
            gb_usb_data.Controls.Add(lb_cur_h);
            gb_usb_data.Controls.Add(lb_cur_time);
            gb_usb_data.Controls.Add(lb_done);
            gb_usb_data.Controls.Add(lb_end_date);
            gb_usb_data.Controls.Add(lb_end_h);
            gb_usb_data.Controls.Add(lb_end_time);
            gb_usb_data.Controls.Add(lb_heartbeatCount);
            gb_usb_data.Controls.Add(lb_message);
            gb_usb_data.Controls.Add(lb_missing_h);
            gb_usb_data.Controls.Add(lb_percent);
            gb_usb_data.Controls.Add(lb_rec_done);
            gb_usb_data.Controls.Add(lb_rec_rpm);
            gb_usb_data.Controls.Add(lb_records_h);
            gb_usb_data.Controls.Add(lb_start_date);
            gb_usb_data.Controls.Add(lb_start_h);
            gb_usb_data.Controls.Add(lb_start_time);
            gb_usb_data.Controls.Add(lb_timeLeft);
            gb_usb_data.Controls.Add(pb_done);
            gb_usb_data.Controls.Add(re_box);
            gb_usb_data.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            gb_usb_data.ForeColor = Color.Black;
            gb_usb_data.Location = new Point(4, 0x84);
            gb_usb_data.Name = "gb_usb_data";
            gb_usb_data.Size = new Size(0x1c8, 0x229);
            gb_usb_data.TabIndex = 6;
            gb_usb_data.TabStop = false;
            gb_usb_data.Text = "WMR200";
            toolTip1.SetToolTip(gb_usb_data, "Scroll the text above");
            lb_missing.AutoSize = true;
            lb_missing.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_missing.Location = new Point(0x53, 0x42);
            lb_missing.Name = "lb_missing";
            lb_missing.Size = new Size(0x24, 13);
            lb_missing.TabIndex = 0x67;
            lb_missing.Text = "0 pkts";
            toolTip1.SetToolTip(lb_missing, "Number of expected history \r\nrecords that are missing.");
            lb_junk.AutoSize = true;
            lb_junk.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_junk.Location = new Point(0x132, 0x42);
            lb_junk.Name = "lb_junk";
            lb_junk.Size = new Size(0x2f, 13);
            lb_junk.TabIndex = 0x69;
            lb_junk.Text = "0 frames";
            toolTip1.SetToolTip(lb_junk, "Junk frames received \r\nfrom the WMR200.");
            lb_rec_time.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_rec_time.ForeColor = Color.SteelBlue;
            lb_rec_time.Location = new Point(0xa9, 0x20);
            lb_rec_time.Name = "lb_rec_time";
            lb_rec_time.Size = new Size(0x52, 13);
            lb_rec_time.TabIndex = 0x6b;
            lb_rec_time.Text = "0d 0h 0m";
            lb_rec_time.TextAlign = ContentAlignment.TopCenter;
            toolTip1.SetToolTip(lb_rec_time,
                                "Timespan of history records\r\nin the WMR200 data logger\r\nin days, hours and minutes.");
            lb_corrupt.AutoSize = true;
            lb_corrupt.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_corrupt.Location = new Point(0x10a, 0x42);
            lb_corrupt.Name = "lb_corrupt";
            lb_corrupt.Size = new Size(0x2c, 13);
            lb_corrupt.TabIndex = 0x68;
            lb_corrupt.Text = "Corrupt:";
            toolTip1.SetToolTip(lb_corrupt, "Junk frames received \r\nfrom the WMR200.");
            lb_cur_date.AutoSize = true;
            lb_cur_date.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_cur_date.ForeColor = Color.Blue;
            lb_cur_date.Location = new Point(0x2b, 0x20);
            lb_cur_date.Name = "lb_cur_date";
            lb_cur_date.Size = new Size(0x3d, 13);
            lb_cur_date.TabIndex = 40;
            lb_cur_date.Text = "01-01-2010";
            lb_cur_date.TextAlign = ContentAlignment.TopRight;
            toolTip1.SetToolTip(lb_cur_date, "Current history records");
            lb_cur_h.AutoSize = true;
            lb_cur_h.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            lb_cur_h.ForeColor = Color.Blue;
            lb_cur_h.Location = new Point(6, 0x20);
            lb_cur_h.Name = "lb_cur_h";
            lb_cur_h.Size = new Size(0x22, 13);
            lb_cur_h.TabIndex = 0x27;
            lb_cur_h.Text = "Curr:";
            toolTip1.SetToolTip(lb_cur_h, "Current history records");
            lb_cur_time.AutoSize = true;
            lb_cur_time.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_cur_time.ForeColor = Color.Blue;
            lb_cur_time.Location = new Point(0x65, 0x20);
            lb_cur_time.Name = "lb_cur_time";
            lb_cur_time.Size = new Size(0x33, 13);
            lb_cur_time.TabIndex = 0x29;
            lb_cur_time.Text = "@ 12h00";
            toolTip1.SetToolTip(lb_cur_time, "Current history records");
            lb_done.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            lb_done.ForeColor = Color.Green;
            lb_done.Location = new Point(0x10a, 0x10);
            lb_done.Name = "lb_done";
            lb_done.Size = new Size(100, 13);
            lb_done.TabIndex = 30;
            lb_done.Text = "Time left";
            lb_done.TextAlign = ContentAlignment.TopCenter;
            toolTip1.SetToolTip(lb_done,
                                "PROGRESS:\r\nEstimated time remaining for history\r\ndownload and percentage completed.");
            lb_end_date.AutoSize = true;
            lb_end_date.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_end_date.ForeColor = Color.Crimson;
            lb_end_date.Location = new Point(0x2b, 0x2f);
            lb_end_date.Name = "lb_end_date";
            lb_end_date.Size = new Size(0x3d, 13);
            lb_end_date.TabIndex = 0x2b;
            lb_end_date.Text = "01-01-2010";
            lb_end_date.TextAlign = ContentAlignment.TopRight;
            toolTip1.SetToolTip(lb_end_date, "Expected last history records");
            lb_end_h.AutoSize = true;
            lb_end_h.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            lb_end_h.ForeColor = Color.Crimson;
            lb_end_h.Location = new Point(6, 0x2f);
            lb_end_h.Name = "lb_end_h";
            lb_end_h.Size = new Size(0x21, 13);
            lb_end_h.TabIndex = 0x2a;
            lb_end_h.Text = "End:";
            toolTip1.SetToolTip(lb_end_h, "Expected last history records");
            lb_end_time.AutoSize = true;
            lb_end_time.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_end_time.ForeColor = Color.Crimson;
            lb_end_time.Location = new Point(0x65, 0x2f);
            lb_end_time.Name = "lb_end_time";
            lb_end_time.Size = new Size(0x33, 13);
            lb_end_time.TabIndex = 0x2c;
            lb_end_time.Text = "@ 12h00";
            toolTip1.SetToolTip(lb_end_time, "Expected last history records");
            lb_heartbeatCount.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            lb_heartbeatCount.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Italic, GraphicsUnit.Point, 0);
            lb_heartbeatCount.ForeColor = Color.DimGray;
            lb_heartbeatCount.Location = new Point(0x273, 0x3ec);
            lb_heartbeatCount.Name = "lb_heartbeatCount";
            lb_heartbeatCount.Size = new Size(80, 13);
            lb_heartbeatCount.TabIndex = 0x65;
            lb_heartbeatCount.Text = "0 HB's";
            lb_heartbeatCount.TextAlign = ContentAlignment.TopRight;
            toolTip1.SetToolTip(lb_heartbeatCount, "Total heartbeats");
            lb_message.BackColor = SystemColors.Info;
            lb_message.BorderStyle = BorderStyle.FixedSingle;
            lb_message.Dock = DockStyle.Bottom;
            lb_message.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_message.FormattingEnabled = true;
            lb_message.Items.AddRange(new object[]
                {"", "", "                             Courtesy of Rainer Finkeldeh www.bashewa.com", ""});
            lb_message.Location = new Point(3, 0x1e3);
            lb_message.Name = "lb_message";
            lb_message.ScrollAlwaysVisible = true;
            lb_message.Size = new Size(450, 0x43);
            lb_message.TabIndex = 6;
            toolTip1.SetToolTip(lb_message, "Messages");
            lb_missing_h.AutoSize = true;
            lb_missing_h.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_missing_h.Location = new Point(6, 0x42);
            lb_missing_h.Name = "lb_missing_h";
            lb_missing_h.Size = new Size(80, 13);
            lb_missing_h.TabIndex = 0x66;
            lb_missing_h.Text = "Missing History:";
            toolTip1.SetToolTip(lb_missing_h, "Number of expected history \r\nrecords that are missing.");
            lb_percent.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_percent.ForeColor = Color.MediumSeaGreen;
            lb_percent.Location = new Point(0x107, 0x20);
            lb_percent.Name = "lb_percent";
            lb_percent.Size = new Size(0x23, 13);
            lb_percent.TabIndex = 100;
            lb_percent.Text = "100%";
            toolTip1.SetToolTip(lb_percent,
                                "PROGRESS:\r\nEstimated time remaining for history\r\ndownload and percentage completed.");
            lb_rec_done.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_rec_done.ForeColor = Color.Blue;
            lb_rec_done.Location = new Point(0xa9, 0x2f);
            lb_rec_done.Name = "lb_rec_done";
            lb_rec_done.Size = new Size(0x52, 13);
            lb_rec_done.TabIndex = 0x25;
            lb_rec_done.Text = "0 of 0";
            lb_rec_done.TextAlign = ContentAlignment.TopCenter;
            toolTip1.SetToolTip(lb_rec_done,
                                "RECORDS:\r\nEstimated number of records to \r\ndownload, progress and download \r\nspeed in records/minute.");
            lb_rec_rpm.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_rec_rpm.ForeColor = Color.Blue;
            lb_rec_rpm.Location = new Point(0xa9, 0x40);
            lb_rec_rpm.Name = "lb_rec_rpm";
            lb_rec_rpm.Size = new Size(0x52, 13);
            lb_rec_rpm.TabIndex = 0x1f;
            lb_rec_rpm.Text = "0 recs/min";
            lb_rec_rpm.TextAlign = ContentAlignment.TopCenter;
            toolTip1.SetToolTip(lb_rec_rpm,
                                "RECORDS:\r\nEstimated number of records to \r\ndownload, progress and download \r\nspeed in records/minute.");
            lb_records_h.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            lb_records_h.ForeColor = Color.Blue;
            lb_records_h.Location = new Point(180, 0x10);
            lb_records_h.Name = "lb_records_h";
            lb_records_h.Size = new Size(60, 13);
            lb_records_h.TabIndex = 0x1d;
            lb_records_h.Text = "Records";
            lb_records_h.TextAlign = ContentAlignment.TopCenter;
            toolTip1.SetToolTip(lb_records_h,
                                "RECORDS:\r\nEstimated number of records to \r\ndownload, progress and download \r\nspeed in records/minute.");
            lb_start_date.AutoSize = true;
            lb_start_date.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_start_date.ForeColor = Color.Green;
            lb_start_date.Location = new Point(0x2b, 0x11);
            lb_start_date.Name = "lb_start_date";
            lb_start_date.Size = new Size(0x3d, 13);
            lb_start_date.TabIndex = 0x23;
            lb_start_date.Text = "01-01-2010";
            lb_start_date.TextAlign = ContentAlignment.TopRight;
            toolTip1.SetToolTip(lb_start_date, "Start of history records");
            lb_start_h.AutoSize = true;
            lb_start_h.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            lb_start_h.ForeColor = Color.Green;
            lb_start_h.Location = new Point(6, 0x11);
            lb_start_h.Name = "lb_start_h";
            lb_start_h.Size = new Size(0x26, 13);
            lb_start_h.TabIndex = 0x22;
            lb_start_h.Text = "Start:";
            toolTip1.SetToolTip(lb_start_h, "Start of history records");
            lb_start_time.AutoSize = true;
            lb_start_time.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_start_time.ForeColor = Color.Green;
            lb_start_time.Location = new Point(0x65, 0x11);
            lb_start_time.Name = "lb_start_time";
            lb_start_time.Size = new Size(0x33, 13);
            lb_start_time.TabIndex = 0x24;
            lb_start_time.Text = "@ 12h00";
            toolTip1.SetToolTip(lb_start_time, "Start of history records");
            lb_timeLeft.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            lb_timeLeft.ForeColor = Color.Green;
            lb_timeLeft.Location = new Point(0x132, 0x20);
            lb_timeLeft.Name = "lb_timeLeft";
            lb_timeLeft.Size = new Size(0x41, 13);
            lb_timeLeft.TabIndex = 0x20;
            lb_timeLeft.Text = "Done";
            lb_timeLeft.TextAlign = ContentAlignment.TopRight;
            toolTip1.SetToolTip(lb_timeLeft,
                                "PROGRESS:\r\nEstimated time remaining for history\r\ndownload and percentage completed.");
            pb_done.Location = new Point(0x10c, 0x2f);
            pb_done.Name = "pb_done";
            pb_done.RightToLeftLayout = true;
            pb_done.Size = new Size(100, 14);
            pb_done.TabIndex = 0x21;
            toolTip1.SetToolTip(pb_done, "Estimated time left for download");
            pb_done.Value = 100;
            re_box.AcceptsTab = true;
            re_box.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            re_box.BorderStyle = BorderStyle.FixedSingle;
            re_box.Font = new Font("Courier New", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            re_box.ForeColor = Color.Black;
            re_box.Location = new Point(6, 0x54);
            re_box.Name = "re_box";
            re_box.ScrollBars = RichTextBoxScrollBars.Vertical;
            re_box.Size = new Size(0x16c, 0x173);
            re_box.TabIndex = 0x13;
            toolTip1.SetToolTip(re_box, "NOTE:\r\nYou can make notes in this box.");
            re_box.Click += re_box_Click;
            gb_wx_data.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            gb_wx_data.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            gb_wx_data.Controls.Add(dg_history);
            gb_wx_data.Location = new Point(0x22f, 4);
            gb_wx_data.Name = "gb_wx_data";
            gb_wx_data.Size = new Size(0x1a6, 0x289);
            gb_wx_data.TabIndex = 7;
            gb_wx_data.TabStop = false;
            gb_wx_data.Text = "Weather Data Received";
            timer_heartbeat.Interval = 0x7148;
            timer_heartbeat.Tick += timer_heartbeat_Tick;
            timer_timeout.Interval = 0x7530;
            timer_timeout.Tick += timer_timeout_Tick;
            btn_start.AutoSize = true;
            btn_start.Cursor = Cursors.Hand;
            btn_start.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            btn_start.ForeColor = Color.ForestGreen;
            btn_start.Location = new Point(0x182, 0x25);
            btn_start.Name = "btn_start";
            btn_start.Size = new Size(0x4b, 0x17);
            btn_start.TabIndex = 0x69;
            btn_start.Tag = "STRT";
            btn_start.Text = "START";
            toolTip1.SetToolTip(btn_start,
                                "START:\r\nStart data extraction.\r\nWMR200 data logger is\r\ndisabled during this process.");
            btn_start.UseVisualStyleBackColor = true;
            btn_start.Click += btn_start_Click;
            btn_stop.AutoSize = true;
            btn_stop.Cursor = Cursors.Hand;
            btn_stop.Enabled = false;
            btn_stop.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            btn_stop.ForeColor = Color.Crimson;
            btn_stop.Location = new Point(0x181, 0x40);
            btn_stop.Name = "btn_stop";
            btn_stop.Size = new Size(0x4b, 0x17);
            btn_stop.TabIndex = 0x6c;
            btn_stop.Tag = "STOP";
            btn_stop.Text = "STOP";
            toolTip1.SetToolTip(btn_stop, "STOP:\r\nStop data extraction and\r\nenable Data Logging.");
            btn_stop.UseVisualStyleBackColor = true;
            btn_stop.Click += btn_stop_Click;
            btn_erase.AutoSize = true;
            btn_erase.Cursor = Cursors.Hand;
            btn_erase.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            btn_erase.ForeColor = Color.Red;
            btn_erase.Location = new Point(0x181, 0x68);
            btn_erase.Name = "btn_erase";
            btn_erase.Size = new Size(0x4b, 0x17);
            btn_erase.TabIndex = 0x6b;
            btn_erase.Tag = "BUTN";
            btn_erase.Text = "ERASE";
            toolTip1.SetToolTip(btn_erase, "WARNING:\r\nYou're about to clear the\r\nWMR200's Data Logger.");
            btn_erase.UseVisualStyleBackColor = true;
            btn_erase.Click += btn_erase_Click;
            gb_dataLogger.Controls.Add(rb_DL10);
            gb_dataLogger.Controls.Add(rb_DL5);
            gb_dataLogger.Controls.Add(rb_DL2);
            gb_dataLogger.Controls.Add(rb_DL1);
            gb_dataLogger.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            gb_dataLogger.Location = new Point(0x127, 0x1f);
            gb_dataLogger.Name = "gb_dataLogger";
            gb_dataLogger.Size = new Size(0x51, 0x5f);
            gb_dataLogger.TabIndex = 0x6a;
            gb_dataLogger.TabStop = false;
            gb_dataLogger.Text = "Data Logger";
            toolTip1.SetToolTip(gb_dataLogger,
                                "DATA LOGGER SETUP:\r\nYour WMR200's Data Logger\r\nfrequency setting for determinig\r\nthe expected number of records.");
            rb_DL10.AutoSize = true;
            rb_DL10.Location = new Point(7, 70);
            rb_DL10.Name = "rb_DL10";
            rb_DL10.Size = new Size(0x47, 0x11);
            rb_DL10.TabIndex = 3;
            rb_DL10.Tag = "10";
            rb_DL10.Text = "10 minute";
            toolTip1.SetToolTip(rb_DL10,
                                "DATA LOGGER SETUP:\r\nYour WMR200's Data Logger\r\nfrequency setting for determinig\r\nthe expected number of records.");
            rb_DL10.UseVisualStyleBackColor = true;
            rb_DL10.Click += rb_DL10_CheckedChanged;
            rb_DL5.AutoSize = true;
            rb_DL5.Location = new Point(7, 0x35);
            rb_DL5.Name = "rb_DL5";
            rb_DL5.Size = new Size(0x47, 0x11);
            rb_DL5.TabIndex = 2;
            rb_DL5.Tag = "5";
            rb_DL5.Text = "  5 minute";
            toolTip1.SetToolTip(rb_DL5,
                                "DATA LOGGER SETUP:\r\nYour WMR200's Data Logger\r\nfrequency setting for determinig\r\nthe expected number of records.");
            rb_DL5.UseVisualStyleBackColor = true;
            rb_DL5.Click += rb_DL5_CheckedChanged;
            rb_DL2.AutoSize = true;
            rb_DL2.Location = new Point(7, 0x24);
            rb_DL2.Name = "rb_DL2";
            rb_DL2.Size = new Size(0x47, 0x11);
            rb_DL2.TabIndex = 1;
            rb_DL2.Tag = "2";
            rb_DL2.Text = "  2 minute";
            toolTip1.SetToolTip(rb_DL2,
                                "DATA LOGGER SETUP:\r\nYour WMR200's Data Logger\r\nfrequency setting for determinig\r\nthe expected number of records.");
            rb_DL2.UseVisualStyleBackColor = true;
            rb_DL2.Click += rb_DL2_CheckedChanged;
            rb_DL1.AutoSize = true;
            rb_DL1.Checked = true;
            rb_DL1.Location = new Point(7, 0x13);
            rb_DL1.Name = "rb_DL1";
            rb_DL1.Size = new Size(0x47, 0x11);
            rb_DL1.TabIndex = 0;
            rb_DL1.TabStop = true;
            rb_DL1.Tag = "1";
            rb_DL1.Text = "  1 minute";
            toolTip1.SetToolTip(rb_DL1,
                                "DATA LOGGER SETUP:\r\nYour WMR200's Data Logger\r\nfrequency setting for determinig\r\nthe expected number of records.");
            rb_DL1.UseVisualStyleBackColor = true;
            rb_DL1.Click += rb_DL1_CheckedChanged;
            errorProvider1.ContainerControl = this;
            menuStrip1.BackColor = Color.Silver;
            menuStrip1.Items.AddRange(new ToolStripItem[]
                {fileToolStripMenuItem, updateToolStripMenuItem, aboutToolStripMenuItem, configToolStripMenuItem});
            menuStrip1.Location = new Point(0, 0);
            menuStrip1.Name = "menuStrip1";
            menuStrip1.Size = new Size(0x3d8, 0x18);
            menuStrip1.TabIndex = 0x68;
            menuStrip1.Text = "menuStrip1";
            fileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] {quitToolStripMenuItem});
            fileToolStripMenuItem.Name = "fileToolStripMenuItem";
            fileToolStripMenuItem.Size = new Size(0x25, 20);
            fileToolStripMenuItem.Text = "File";
            quitToolStripMenuItem.Name = "quitToolStripMenuItem";
            quitToolStripMenuItem.Size = new Size(0x61, 0x16);
            quitToolStripMenuItem.Text = "Quit";
            quitToolStripMenuItem.Click += quitToolStripMenuItem_Click;
            updateToolStripMenuItem.BackColor = Color.Silver;
            updateToolStripMenuItem.Name = "updateToolStripMenuItem";
            updateToolStripMenuItem.Size = new Size(0x39, 20);
            updateToolStripMenuItem.Text = "Update";
            updateToolStripMenuItem.TextAlign = ContentAlignment.MiddleLeft;
            updateToolStripMenuItem.ToolTipText = "Check for new version";
            updateToolStripMenuItem.Click += btn_update_Click;
            aboutToolStripMenuItem.Name = "aboutToolStripMenuItem";
            aboutToolStripMenuItem.Size = new Size(0x34, 20);
            aboutToolStripMenuItem.Text = "About";
            aboutToolStripMenuItem.TextAlign = ContentAlignment.MiddleLeft;
            aboutToolStripMenuItem.Click += btn_about_Click;
            configToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[]
                {wDAutoStartToolStripMenuItem, startWDToolStripMenuItem});
            configToolStripMenuItem.Name = "configToolStripMenuItem";
            configToolStripMenuItem.Size = new Size(0x37, 20);
            configToolStripMenuItem.Text = "Config";
            configToolStripMenuItem.TextAlign = ContentAlignment.MiddleLeft;
            wDAutoStartToolStripMenuItem.CheckOnClick = true;
            wDAutoStartToolStripMenuItem.Name = "wDAutoStartToolStripMenuItem";
            wDAutoStartToolStripMenuItem.Size = new Size(0x98, 0x16);
            wDAutoStartToolStripMenuItem.Text = "Auto start WD";
            wDAutoStartToolStripMenuItem.ToolTipText = "Automatically start WD after \r\nhistory has been collected.";
            wDAutoStartToolStripMenuItem.Click += wDAutoStartToolStripMenuItem_Click;
            startWDToolStripMenuItem.Name = "startWDToolStripMenuItem";
            startWDToolStripMenuItem.Size = new Size(0x98, 0x16);
            startWDToolStripMenuItem.Text = "Start WD now";
            startWDToolStripMenuItem.ToolTipText = "Start Weather Display now";
            startWDToolStripMenuItem.Click += startWDToolStripMenuItem_Click;
            pictureBox1.Location = new Point(10, 0x20);
            pictureBox1.Name = "pictureBox1";
            pictureBox1.Size = new Size(0x111, 0x5f);
            pictureBox1.TabIndex = 0x6d;
            pictureBox1.TabStop = false;
            usb.ProductId = 0;
            usb.VendorId = 0;
            usb.OnSpecifiedDeviceArrived += usb_OnSpecifiedDeviceArrived;
            usb.OnSpecifiedDeviceRemoved += usb_OnSpecifiedDeviceRemoved;
            usb.OnDeviceArrived += usb_OnDeviceArrived;
            usb.OnDeviceRemoved += usb_OnDeviceRemoved;
            usb.OnDataReceived += usb_OnDataReceived;
            usb.OnDataSend += usb_OnDataSend;
            gauge_rpm.BaseArcColor = Color.DarkGray;
            gauge_rpm.BaseArcRadius = 0x35;
            gauge_rpm.BaseArcStart = 180;
            gauge_rpm.BaseArcSweep = 90;
            gauge_rpm.BaseArcWidth = 2;
            gauge_rpm.Cap_Idx = 1;
            gauge_rpm.CapColors = new[] {Color.Black, Color.Black, Color.Black, Color.Black, Color.Black};
            gauge_rpm.CapPosition = new Point(30, 0x36);
            gauge_rpm.CapsPosition = new[]
                {new Point(10, 10), new Point(30, 0x36), new Point(10, 10), new Point(10, 10), new Point(10, 10)};
            gauge_rpm.CapsText = new[] {"", "RPM", "", "", ""};
            gauge_rpm.CapText = "RPM";
            gauge_rpm.Center = new Point(0x43, 0x41);
            gauge_rpm.Font = new Font("Mufferaw", 8.249999f, FontStyle.Regular, GraphicsUnit.Point, 0);
            gauge_rpm.Location = new Point(0x177, 11);
            gauge_rpm.MaxValue = 200f;
            gauge_rpm.MinValue = 0f;
            gauge_rpm.Name = "gauge_rpm";
            gauge_rpm.NeedleColor1 = AGauge.NeedleColorEnum.Green;
            gauge_rpm.NeedleColor2 = Color.ForestGreen;
            gauge_rpm.NeedleRadius = 0x35;
            gauge_rpm.NeedleType = 1;
            gauge_rpm.NeedleWidth = 2;
            gauge_rpm.Range_Idx = 0;
            gauge_rpm.RangeColor = Color.LightGreen;
            gauge_rpm.RangeEnabled = true;
            gauge_rpm.RangeEndValue = 200f;
            gauge_rpm.RangeInnerRadius = 0x2b;
            gauge_rpm.RangeOuterRadius = 0x35;
            gauge_rpm.RangesColor = new[]
                {
                    Color.LightGreen, Color.FromArgb(0xff, 0xff, 0x80), SystemColors.Control, Color.LightSteelBlue,
                    SystemColors.Control
                };
            gauge_rpm.RangesEnabled = new[] {true, true, false, true, false};
            gauge_rpm.RangesEndValue = new[] {200f, 80f, 0f, 200f, 0f};
            gauge_rpm.RangesInnerRadius = new[] {0x2b, 0x2b, 70, 0x2b, 70};
            gauge_rpm.RangesOuterRadius = new[] {0x35, 0x35, 80, 0x35, 80};
            var numArray = new float[5];
            numArray[0] = 80f;
            numArray[3] = 150f;
            gauge_rpm.RangesStartValue = numArray;
            gauge_rpm.RangeStartValue = 80f;
            gauge_rpm.ScaleLinesInterColor = Color.DimGray;
            gauge_rpm.ScaleLinesInterInnerRadius = 0x35;
            gauge_rpm.ScaleLinesInterOuterRadius = 0x2b;
            gauge_rpm.ScaleLinesInterWidth = 1;
            gauge_rpm.ScaleLinesMajorColor = Color.DimGray;
            gauge_rpm.ScaleLinesMajorInnerRadius = 0x35;
            gauge_rpm.ScaleLinesMajorOuterRadius = 0x2b;
            gauge_rpm.ScaleLinesMajorStepValue = 50f;
            gauge_rpm.ScaleLinesMajorWidth = 1;
            gauge_rpm.ScaleLinesMinorColor = Color.Gray;
            gauge_rpm.ScaleLinesMinorInnerRadius = 0x30;
            gauge_rpm.ScaleLinesMinorNumOf = 5;
            gauge_rpm.ScaleLinesMinorOuterRadius = 0x35;
            gauge_rpm.ScaleLinesMinorWidth = 1;
            gauge_rpm.ScaleNumbersColor = Color.Green;
            gauge_rpm.ScaleNumbersFormat = null;
            gauge_rpm.ScaleNumbersRadius = 0x3f;
            gauge_rpm.ScaleNumbersRotation = 0;
            gauge_rpm.ScaleNumbersStartScaleLine = 0;
            gauge_rpm.ScaleNumbersStepScaleLines = 1;
            gauge_rpm.Size = new Size(0x4b, 0x47);
            gauge_rpm.TabIndex = 0x6a;
            gauge_rpm.Text = "aGauge1";
            toolTip1.SetToolTip(gauge_rpm, "SPEED:\r\nAverage download speed\r\nin records per minute.");
            gauge_rpm.Value = 0f;
            base.AutoScaleMode = AutoScaleMode.Inherit;
            BackColor = Color.Gainsboro;
            base.ClientSize = new Size(0x3d8, 0x2af);
            base.Controls.Add(pictureBox1);
            base.Controls.Add(btn_start);
            base.Controls.Add(btn_stop);
            base.Controls.Add(btn_erase);
            base.Controls.Add(gb_dataLogger);
            base.Controls.Add(btn_wdexport);
            base.Controls.Add(gb_manual);
            base.Controls.Add(gb_usb_data);
            base.Controls.Add(gb_wx_data);
            base.Controls.Add(menuStrip1);
            Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            base.MainMenuStrip = menuStrip1;
            MaximumSize = new Size(0x5dc, 0x4b0);
            MinimumSize = new Size(480, 0x2d3);
            base.Name = "Wmr200DataLogger";
            base.SizeGripStyle = SizeGripStyle.Show;
            base.StartPosition = FormStartPosition.CenterScreen;
            Text = "WMR200 Data Logger v11.01.03.1";
            ((ISupportInitialize) dg_history).EndInit();
            gb_counts.ResumeLayout(false);
            gb_counts.PerformLayout();
            gb_manual.ResumeLayout(false);
            gb_manual.PerformLayout();
            gb_usb_data.ResumeLayout(false);
            gb_usb_data.PerformLayout();
            gb_wx_data.ResumeLayout(false);
            gb_dataLogger.ResumeLayout(false);
            gb_dataLogger.PerformLayout();
            ((ISupportInitialize) errorProvider1).EndInit();
            menuStrip1.ResumeLayout(false);
            menuStrip1.PerformLayout();
            ((ISupportInitialize) pictureBox1).EndInit();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void log_USB_Frame(byte[] data)
        {
            if (cb_showUSB.Checked)
            {
                var str = "";
                for (var i = 1; i < 9; i++)
                {
                    str = str + ((i > (data[1] + 1)) ? ".. " : (data[i].ToString("X2") + " "));
                }
                print_msg(rec_type + str, true);
            }
        }

        private void log_WMR_Packet(byte[] wmr_data)
        {
            print_msg("--------------------- --------------------", false);
            if (cb_showWMR.Checked && (((wmr_data[1] >= 210) && (wmr_data[1] <= 0xd7)) || (wmr_data[1] == 0xd9)))
            {
                if (wmr_data[1] != 0xd9)
                {
                    mn = wmr_data[3].ToString().PadLeft(2, '0');
                    hh = wmr_data[4].ToString().PadLeft(2, '0');
                    dd = wmr_data[5].ToString().PadLeft(2, '0');
                    mm = wmr_data[6].ToString().PadLeft(2, '0');
                    yy = "20" + wmr_data[7].ToString();
                    pkt_time = yy + "-" + mm + "-" + dd + "  " + hh + ":" + mn + "  ";
                }
                if (wmr_data[1] == 0xd9)
                {
                    print_msg(arrayToHex(wmr_data), true);
                }
                else
                {
                    print_msg(pkt_time + arrayToHex(wmr_data), false);
                }
                print_msg("===================== ====================", false);
            }
        }

        private void msg(string str)
        {
            lb_message.Items.Insert(0, timestamp(false) + str);
        }

        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            usb.RegisterHandle(base.Handle);
        }

        private void print_msg(string msg, bool addDate = true)
        {
            if (cb_showUSB.Checked)
            {
                if (addDate)
                {
                    msg = timestamp(true) + msg;
                }
                re_box.AppendText(msg + "\n");
                if (cb_scroll.Checked)
                {
                    re_box.ScrollToCaret();
                }
            }
        }

        private void process_WMR_Packet(byte[] wmr_data)
        {
            if (!check_CRC(wmr_data))
            {
                print_msg("===================== ========== CRC ERROR", false);
                msg("CRC Error");
            }
            else
            {
                if (gotTimeFix && (histTimeFix != 0M))
                {
                    fix_pkt_timestamp(ref wmr_data);
                }
                log_WMR_Packet(wmr_data);
                switch (wmr_data[1])
                {
                }
            }
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void rb_DL1_CheckedChanged(object sender, EventArgs e)
        {
            datalogger_interval = 1M;
        }

        private void rb_DL10_CheckedChanged(object sender, EventArgs e)
        {
            datalogger_interval = 10M;
        }

        private void rb_DL2_CheckedChanged(object sender, EventArgs e)
        {
            datalogger_interval = 2M;
        }

        private void rb_DL5_CheckedChanged(object sender, EventArgs e)
        {
            datalogger_interval = 5M;
        }

        private void rcv_dataClear()
        {
            Array.Clear(rcv_data, 0, rcv_data.Length);
        }

        private void re_box_Click(object sender, EventArgs e)
        {
        }

        private void send_ACK()
        {
            if (stopIssued)
            {
                heartbeat_stop();
                send_command("01 DF", "CMD ");
            }
            else
            {
                heartbeat_reset();
                if (rb_ackDA.Checked)
                {
                    send_command("01 DA", "CMD ");
                }
            }
        }

        private void send_command(string cmd, string who = "CMD ")
        {
            try
            {
                who = who + ": ";
                cmd = cmd + " ";
                cmd.Trim();
                if (cmd != null)
                {
                    var strArray = cmd.Split(new[] {' '});
                    var data = new byte[9];
                    for (var i = 0; i < strArray.Length; i++)
                    {
                        if (strArray[i] != "")
                        {
                            var num2 = int.Parse(strArray[i], NumberStyles.HexNumber);
                            data[i + 1] = Convert.ToByte(num2);
                            who = who + num2.ToString("X2") + " ";
                        }
                    }
                    if (usb.SpecifiedDevice != null)
                    {
                        usb.SpecifiedDevice.SendData(data);
                        print_msg(who, true);
                        timer_timeout_reset();
                    }
                    else
                    {
                        MessageBox.Show("Sorry but the WMR200 is not present. Plug it in!! ");
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
        }

        private void startWDToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void tb_interval_TextChanged(object sender, EventArgs e)
        {
            heartbeat_interval = Convert.ToInt16(tb_interval.Text);
        }

        private void timer_heartbeat_Tick(object sender, EventArgs e)
        {
            if (!stopIssued)
            {
                print_msg("BEAT: heartbeat (" + heartbeat_interval.ToString() + " seconds)", true);
                send_command("01 D0", "CMD ");
                heartbeatCount++;
                lb_heartbeatCount.Text = heartbeatCount.ToString() + " HB's";
            }
        }

        private void timer_timeout_reset()
        {
            timer_timeout.Stop();
            timer_timeout.Start();
        }

        private void timer_timeout_Tick(object sender, EventArgs e)
        {
            timer_timeout.Stop();
            print_msg("NOTE: " + WMR_timeout.ToString() + " seconds WMR timeout", true);
            msg(WMR_timeout.ToString() + " seconds WMR timeout");
            if (kickstartOnTimeout)
            {
                print_msg("KICK: kickstarted", true);
                send_command("01 D0", "CMD ");
            }
        }

        private string timestamp(bool showMS = true)
        {
            logTime = DateTime.Now;
            var str = logTime.Hour.ToString().PadLeft(2, '0');
            var str2 = logTime.Minute.ToString().PadLeft(2, '0');
            var str3 = logTime.Second.ToString().PadLeft(2, '0');
            var str4 = logTime.Millisecond.ToString().PadLeft(3, '0');
            return (str + ":" + str2 + ":" + str3 + (showMS ? ("." + str4 + " ") : " "));
        }

        private void update_Counters(byte pkt_type, bool checkLen = true)
        {
            if ((rcv_data[0] != 0) && checkLen)
            {
                rec_type = "      ";
            }
            else
            {
                switch (pkt_type)
                {
                    case 0xd1:
                        rec_type = "Ack : ";
                        goto Label_0238;

                    case 210:
                        rec_type = "Hist: ";
                        iHist++;
                        lb_hist.Text = iHist.ToString();
                        goto Label_0238;

                    case 0xd3:
                        rec_type = "Wind: ";
                        iWind++;
                        lb_wind.Text = iWind.ToString();
                        goto Label_0238;

                    case 0xd4:
                        rec_type = "Rain: ";
                        iRain++;
                        lb_rain.Text = iRain.ToString();
                        goto Label_0238;

                    case 0xd5:
                        rec_type = "UVI : ";
                        iUVI++;
                        lb_uvi.Text = iUVI.ToString();
                        goto Label_0238;

                    case 0xd6:
                        rec_type = "Baro: ";
                        iBaro++;
                        lb_baro.Text = iBaro.ToString();
                        goto Label_0238;

                    case 0xd7:
                        rec_type = "Temp: ";
                        iTemp++;
                        lb_temp.Text = iTemp.ToString();
                        goto Label_0238;

                    case 0xd9:
                        rec_type = "Stat: ";
                        iStat++;
                        lb_batt.Text = iStat.ToString();
                        goto Label_0238;

                    case 0xdb:
                        rec_type = "Eras: ";
                        goto Label_0238;

                    case 0xdf:
                        rec_type = "Stop: ";
                        goto Label_0238;
                }
                rec_type = "Junk: ";
                iJunk++;
                lb_junk.Text = iJunk.ToString() + " frames";
            }
            Label_0238:
            iTotal = (((((iHist + iWind) + iRain) + iUVI) + iBaro) + iTemp) + iStat;
            lb_total.Text = iTotal.ToString();
        }

        private void usb_OnDataReceived(object sender, DataReceivedEventArgs args)
        {
            if (base.InvokeRequired)
            {
                try
                {
                    base.Invoke(new DataReceivedEventHandler(usb_OnDataReceived), new[] {sender, args});
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception.ToString());
                }
            }
            else
            {
                update_Counters(args.data[2], true);
                log_USB_Frame(args.data);
                if (((rcv_data[0] != 0) || ((args.data[2] >= 0xd1) && (args.data[2] <= 0xd7))) ||
                    (((args.data[2] == 0xd9) || (args.data[2] == 0xdb)) || (args.data[2] == 0xdf)))
                {
                    Array.Copy(args.data, 2, rcv_data, rcv_data[0] + 1, args.data[1]);
                    rcv_data[0] = (byte) (rcv_data[0] + args.data[1]);
                    if (((rcv_data[2] != 0) && (rcv_data[0] >= rcv_data[2])) &&
                        (((rcv_data[1] >= 210) && (rcv_data[1] <= 0xd7)) || (rcv_data[1] == 0xd9)))
                    {
                        Array.Copy(rcv_data, 1, wmr_data, 1, rcv_data[2]);
                        wmr_data[0] = wmr_data[2];
                        if (rcv_data[0] > rcv_data[2])
                        {
                            var objArray2 = new object[]
                                {
                                    "     (", rcv_data[0], " bytes -> ", (rcv_data[0] - rcv_data[2]).ToString(),
                                    " overflow)"
                                };
                            print_msg(string.Concat(objArray2), true);
                        }
                        if (wmr_data[1] == 210)
                        {
                            if (iHist == 1)
                            {
                                startTime = DateTime.Now;
                                nowTime = DateTime.Now;
                                mn = wmr_data[3].ToString().PadLeft(2, '0');
                                hh = wmr_data[4].ToString().PadLeft(2, '0');
                                dd = wmr_data[5].ToString().PadLeft(2, '0');
                                mm = wmr_data[6].ToString().PadLeft(2, '0');
                                yy = "20" + wmr_data[7].ToString();
                                lb_start_date.Text = dd + "-" + mm + "-" + yy;
                                lb_start_time.Text = "@ " + hh + "h" + mn;
                                msg("History Start Time: " + lb_start_date.Text + " " + lb_start_time.Text);
                                print_msg("History Start: " + lb_start_date.Text + " " + lb_start_time.Text, false);
                                lb_end_date.Text = startTime.Day.ToString().PadLeft(2, '0') + "-" +
                                                   startTime.Month.ToString().PadLeft(2, '0') + "-" +
                                                   startTime.Year.ToString();
                                lb_end_time.Text = "@ " + startTime.Hour.ToString().PadLeft(2, '0') + "h" +
                                                   startTime.Minute.ToString().PadLeft(2, '0');
                                msg("History End Time : " + lb_end_date.Text + " " + lb_end_time.Text);
                                print_msg("History End  : " + lb_end_date.Text + " " + lb_end_time.Text, false);
                                hist_from_date = new DateTime(wmr_data[7] + 0x7d0, wmr_data[6], wmr_data[5], wmr_data[4],
                                                              wmr_data[3], 0);
                                hist_from_date.AddMinutes(((long) histTimeFix));
                                var span = nowTime.Subtract(hist_from_date);
                                tHist = decimal.Round(((decimal) span.TotalMinutes)/datalogger_interval, 0);
                                lb_rec_time.Text = span.Days.ToString() + "d " + span.Hours.ToString() + "h " +
                                                   span.Minutes.ToString() + "m";
                                remainingSecs = decimal.Round(Convert.ToDecimal(tHist)*avgTime, 0);
                                estTime = new TimeSpan(((long) remainingSecs)*0x989680L);
                                lb_timeLeft.Text = estTime.Hours.ToString() + "h " +
                                                   estTime.Minutes.ToString().PadLeft(2, '0') + "m " +
                                                   estTime.Seconds.ToString().PadLeft(2, '0') + "s";
                                rpm = 60M/avgTime;
                                lb_rec_rpm.Text = decimal.Round(rpm, 0).ToString() + " recs/min";
                                gauge_rpm.Value = Convert.ToInt32(Math.Min(decimal.Round(rpm, 0), 200M));
                                gauge_rpm.Cap_Idx = 1;
                                gauge_rpm.CapText = gauge_rpm.Value.ToString();
                                msg("Expected download time: " + lb_timeLeft.Text + "ec  for " + tHist.ToString() +
                                    " records (" + remainingSecs.ToString() + " seconds)");
                                print_msg("Expected Time: " + lb_timeLeft.Text + " (at " + lb_rec_rpm.Text + ")", false);
                                print_msg("Expected Recs: " + tHist.ToString(), false);
                                print_msg(
                                    "Hist Time Fix: " +
                                    decimal.Round(Convert.ToDecimal(timeCorrection.TotalMinutes), 0).ToString() +
                                    " minutes", false);
                            }
                            else if ((iHist%20) == 0)
                            {
                                nowTime = DateTime.Now;
                                avgTime = Convert.ToDecimal(nowTime.Subtract(startTime).TotalSeconds)/
                                          Convert.ToDecimal(iHist);
                                rpm = 60M/avgTime;
                                lb_rec_rpm.Text = decimal.Round(rpm, 0).ToString() + " recs/min";
                                gauge_rpm.Value = Convert.ToInt32(Math.Min(decimal.Round(rpm, 0), 200M));
                                gauge_rpm.Cap_Idx = 1;
                                gauge_rpm.CapText = Math.Round(rpm, 0).ToString();
                            }
                            remainingSecs = decimal.Round(Convert.ToDecimal((tHist - iHist))*avgTime, 0);
                            estTime = new TimeSpan(((long) remainingSecs)*0x989680L);
                            lb_timeLeft.Text = estTime.Hours.ToString() + "h " +
                                               estTime.Minutes.ToString().PadLeft(2, '0') + "m " +
                                               estTime.Seconds.ToString().PadLeft(2, '0') + "s";
                            if (tHist > 0M)
                            {
                                var d = (iHist/tHist)*100M;
                                pb_done.Value = (int) decimal.Round(d);
                                lb_percent.Text = pb_done.Value.ToString() + "%";
                                lb_rec_done.Text = iHist.ToString() + " of " + tHist.ToString();
                            }
                            lb_cur_date.Text = dd + "-" + mm + "-" + yy;
                            lb_cur_time.Text = "@ " + hh + "h" + mn;
                        }
                        else if (!gotTimeFix && (wmr_data[1] <= 0xd7))
                        {
                            nowTime = DateTime.Now;
                            nowTime = new DateTime(nowTime.Year, nowTime.Month, nowTime.Day, nowTime.Hour,
                                                   nowTime.Minute, 0);
                            wmrTime = new DateTime(wmr_data[7] + 0x7d0, wmr_data[6], wmr_data[5], wmr_data[4],
                                                   wmr_data[3], 0);
                            timeCorrection = nowTime.Subtract(wmrTime);
                            histTimeFix = decimal.Ceiling(Convert.ToDecimal(timeCorrection.TotalMinutes));
                            msg("WMR Time Difference: " + Math.Abs(histTimeFix).ToString() + " minutes " +
                                ((histTimeFix < 0M) ? "fast" : "slow"));
                            print_msg(
                                "Pkts Time Fix: " +
                                decimal.Round(Convert.ToDecimal(timeCorrection.TotalMinutes), 0).ToString() + " minutes",
                                false);
                            gotTimeFix = true;
                        }
                        if (rcv_data[0] > rcv_data[2])
                        {
                            var length = Convert.ToByte((rcv_data[0] - rcv_data[2]));
                            msg("NOTE: Packet overflow: " + length.ToString() + " bytes");
                            print_msg("NOTE: Packet overflow " + length.ToString() + " bytes", true);
                            Array.Copy(rcv_data, rcv_data[2] + 1, rcv_data, 1, length);
                            rcv_data[0] = length;
                            Array.Clear(rcv_data, length + 1, (rcv_data.Length - length) - 1);
                            update_Counters(rcv_data[1], false);
                            print_msg(rec_type + length.ToString().PadLeft(2, '0') + " " + arrayToHex(rcv_data), true);
                            if (((rcv_data[1] < 0xd1) || (rcv_data[1] > 0xd7)) &&
                                (((rcv_data[1] != 0xd9) && (rcv_data[1] != 0xdb)) && (rcv_data[1] != 0xdf)))
                            {
                                rcv_dataClear();
                            }
                            else if (rcv_data[1] == 0xd1)
                            {
                                rcv_dataClear();
                                if (iHist > 0)
                                {
                                    send_ACK();
                                }
                            }
                            else if (rcv_data[1] == 0xdf)
                            {
                                timer_timeout.Stop();
                                rcv_dataClear();
                                msg("WMR200 acknowledged Stop Communication");
                                stopIssued = true;
                            }
                        }
                        else
                        {
                            var num26 = wmr_data[1];
                            if (num26 == 210)
                            {
                                if (rb_ackDA.Checked)
                                {
                                    inHistory = true;
                                    send_ACK();
                                }
                                iLive = 0;
                                rcv_dataClear();
                            }
                            else
                            {
                                if (inHistory)
                                {
                                    int num27;
                                    iLive = (num27 = iLive) + 1;
                                    if (num27 >= 10)
                                    {
                                        pb_done.Value = 100;
                                        lb_percent.Text = "100%";
                                        lb_rec_done.Text = iHist.ToString() + " of " + tHist.ToString();
                                        lb_missing.Text = ((tHist - iHist)).ToString() + " packets";
                                        lb_timeLeft.Text = "Done";
                                        msg("History extraction complete");
                                        print_msg("FINI: history complete", true);
                                        if (!stopIssued)
                                        {
                                            heartbeat_start();
                                            send_command("01 D0", "CMD ");
                                        }
                                        inHistory = false;
                                        iLive = 0;
                                    }
                                    else if (gotTimeFix && cb_ackLIVE.Checked)
                                    {
                                        send_ACK();
                                    }
                                }
                                rcv_dataClear();
                            }
                        }
                        process_WMR_Packet(wmr_data);
                    }
                    else if (rcv_data[0] == 1)
                    {
                        var num29 = rcv_data[1];
                        switch (num29)
                        {
                            case 0xd1:
                                rcv_dataClear();
                                if (iHist > 0)
                                {
                                    send_ACK();
                                }
                                return;

                            case 0xdb:
                                timer_timeout.Stop();
                                rcv_dataClear();
                                msg("WMR200 acknowleged Data Logger cleared");
                                return;
                        }
                        if (num29 == 0xdf)
                        {
                            timer_timeout.Stop();
                            rcv_dataClear();
                            msg("WMR200 acknowledged Stop Communication");
                            stopIssued = true;
                        }
                    }
                }
            }
        }

        private void usb_OnDataSend(object sender, EventArgs e)
        {
        }

        private void usb_OnDeviceArrived(object sender, EventArgs e)
        {
            msg("Found a Device");
        }

        private void usb_OnDeviceRemoved(object sender, EventArgs e)
        {
            if (base.InvokeRequired)
            {
                base.Invoke(new EventHandler(usb_OnDeviceRemoved), new[] {sender, e});
            }
            else
            {
                msg("Device was removed");
            }
        }

        private void usb_OnSpecifiedDeviceArrived(object sender, EventArgs e)
        {
            msg("WMR200 device was found: " + (usb.SpecifiedDevice.OutputReportLength - 1) + " byte machine");
            btnConnect.Enabled = false;
            btn_reset.Enabled = true;
            if (autoStart)
            {
                cmd_reset();
                cmd_sendD0();
            }
            else
            {
                btn_reset.Focus();
            }
        }

        private void usb_OnSpecifiedDeviceRemoved(object sender, EventArgs e)
        {
            if (base.InvokeRequired)
            {
                base.Invoke(new EventHandler(usb_OnSpecifiedDeviceRemoved), new[] {sender, e});
            }
            else
            {
                msg(DateTime.Now + " WMR200 was unplugged");
                btnConnect.Enabled = true;
                btn_start.Enabled = true;
                btn_stop.Enabled = false;
                btn_reset.Enabled = false;
                btn_sendD0.Enabled = false;
                btn_sendDA.Enabled = false;
                btn_erase.Enabled = false;
                btn_sendDF.Enabled = false;
                btn_send.Enabled = false;
                autoStart = false;
            }
        }

        private void wDAutoStartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var str = wDAutoStartToolStripMenuItem.CheckOnClick.ToString();
            Application.UserAppDataRegistry.SetValue("autoStartWD", str);
        }

        protected override void WndProc(ref Message m)
        {
            usb.ParseMessages(ref m);
            base.WndProc(ref m);
        }
    }
}